Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Python: universal newlines with file from CGI?

Python: universal newlines with file from CGI?
Thread Tools
nonhuman
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Dec 11, 2007, 11:12 AM
 
I'm working on a Python CGI script that operates on an uploaded text file. I ran into a problem with a particular file, and experimenting with it at the command line has revealed that the only thing that seems to make it work properly is if I open the file in universal-newline mode (open(file, "U")). However since I'm getting the file from formdata, I have no idea how I could possibly use universal-newline mode with it. Extensive googling has revealed only other people asking the same question and getting no answer.

Does anyone here have any ideas?
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Dec 11, 2007, 11:34 AM
 
There are two basic solutions:

1) Write the data out to a temp file, and then open that.

2) Use regular expressions to make the newlines the way you want. You are probably looking at three combinations: windows (\r\n), old mac (\r), *nix (\n). You probably just want to convert everything to a single type (probably the last one). You want to probably start with the windows type when converting.

I am ignoring the Unicode returns, and the few other types, but that should probably get you where you want to go.
     
nonhuman  (op)
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Dec 11, 2007, 11:37 AM
 
That's what I was afraid of.

Thanks.
     
nonhuman  (op)
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Dec 11, 2007, 12:09 PM
 
Argh. After all that it turned out not to be a newline problem after all, but a user error from people who can't follow simple directions...

Apparently when reading from a CSV file, a row that has too few columns in it is (understandably) interpreted as a newline issue.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 11:19 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,