Reputation: 4797
The contents of the file look like this:
1/15/13,930,1441.5
1/15/13,1000,1442.75
1/15/13,1030,1444
I run:
the_txt_file = open('/txt_file')
Then I run:
the_txt_file_as_a_list = the_txt_file.readlines()
Then I run:
print the_txt_file_as_a_list
And I get this:
['1/15/13,930,1441.5\r1/15/13,1000,1442.75\r1/15/13,1030,1444\r1/']
But I was expecting something like:
['1/15/13,930,1441.5\n','15/13,1000,1442.75\n','15/13,1030,1444\n']
This happens to me pretty frequently, what is going on?
Upvotes: 1
Views: 205
Reputation: 4797
So it seems that the problem had something to do with the way my mac interacted with the .txt file
The problem was fixed by swapping:
the_txt_file = open('/txt_file')
with:
the_txt_file = open('/txt_file', 'rU')
The 'rU' is called 'universal-readline'. Opening a file in 'rU' mode is opening a file in Universal readline mode. Upon running:
the_txt_file_as_a_list = the_txt_file.readlines()
and then:
print the_txt_file_as_a_list
my output went from:
['1/15/13,930,1441.5\r1/15/13,1000,1442.75\r1/15/13,1030,1444\r1/']
to:
['1/15/13,930,1441.5\n', '1/15/13,1000,1442.75\n', '1/15/13,1030,1444\n']
Later, I was able to print each item seperatly by:
for item in the_txt_file_as_a_list:
print item
The output looked like:
1/15/13,930,1441.5
1/15/13,1000,1442.75
1/15/13,1030,1444
Upvotes: 2
Reputation: 106
I would assume that you, or the original creator of this data file were on a Mac. Seems you are expecting it to be a simple '\n' line ending, but suffer from the originating editors system default line ending (most likely).
An easy fix, is to call open(...)
with the rU
option like so:
the_txt_file = open('/txt_file', 'rU')
This ensures that the file is opened read only, and uses Universal newline support when reading the particular file.
Good luck!
Upvotes: 1