Why does Python 2.7.3 think my .csv document is all on one line?
Solution 1
Your file is using \r
as line separators (also known as the "CR" or "Classic Mac" newline convention). Python's open
doesn't deal with this by default.
You can use "universal newlines" mode ('rU'
mode in open
) to open the file properly.
(Note that some Mac text editors still use \r
as the line terminator, though these are thankfully much less common now than they were a few years ago.)
Solution 2
Your input file is poorly formatted. On Linux, lines are separated by '\n'
. On Windows, lines are separated by '\r\n'
, but code in the runtime library makes the '\r'
disappear.
In your file, the lines are separated by '\r'
, which is not a standard in any modern operating system. Perhaps the program that created the file is flawed in some way.
Solution 3
if you're dealing with csv you should use the csv
module, it takes care of most of the crap involved with processing csv input/output.
import csv
with open("example.csv", "rb") as infile:
reader = csv.reader(infile)
for row in reader:
print row # a list of items in your file
The with
statement hear will automatically close the file for you when you drop out of the statement block.
Jeremy Caron
Day Job: Stressed-out graduate student trying to save future me from certain death by onmipresent antibiotic-resistant bacteria in an overcrowded and overmedicated world. Night Job: Amateur coding enthusiast, cat food dispenser. Personal Hero: Dr. James Grimes, for his mastery of YouTube to make math something that I enjoy again. In his words: "As we all know, YouTube is the home of rational and informed debate."
Updated on June 04, 2022Comments
-
Jeremy Caron almost 2 years
I'm new to programming and I encountered a problem in some of my coursework that I can't make any sense of. Consider an imaginary file called 'example.csv' with the following contents.
Key1,Value1 Key2,Value2 Key3,Value3 ...
If I run the following code, it prints every line in the file followed by a single asterisk on the last line. I expected it to print each line separated by an asterisk.
infile = open("example.csv", "r") for line in infile: print line.strip() print '*' #row_elements = line.split(",") #print row_elements
Furthermore, if I try to split the line at each comma by removing the hashes in the above code I get the following output.
['Key1', 'Value1\rKey2', 'Value2\rKey3'...
By instead passing "\r" to the .split() method the output is slightly improved.
['Key1,Value1', 'Key2,Value2'...
I still don't understand why python thinks the entire file is all on one line in the first place. Does anyone have insight into this?
-
matth about 11 yearsTrue, but it doesn't answer his question nor solve his problem. He still needs to open with
"U"
. Perhaps your answer would be better as a comment. -
Jeremy Caron about 11 yearsThis was very helpful and 'rU' mode worked just fine. Thanks!
-
Jeremy Caron about 11 yearsAs I mentioned in the comments on the question, the culprit was Excel for Mac 2011. Thanks for the help.