This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: CSV reader does not parse Mac line endings
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: andrewmcnamara Nosy List: andrewmcnamara, bbum, lmeyn, skip.montanaro
Priority: normal Keywords:

Created on 2003-08-15 21:16 by lmeyn, last changed 2022-04-10 16:10 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
testcsv.py lmeyn, 2003-08-15 21:33 Test code for error, ran under Mac OS X 10.2.6, Python 2.3
Messages (5)
msg17807 - (view) Author: Larry Meyn (lmeyn) Date: 2003-08-15 21:16
#Test code:

import csv
import traceback

class excel_mac(csv.Dialect):
    delimiter = ','
    quotechar = '"'
    doublequote = True
    skipinitialspace = False
    lineterminator = '\r'
    quoting = csv.QUOTE_MINIMAL
csv.register_dialect("excel_mac", excel_mac)

class excel_unix(csv.Dialect):
    delimiter = ','
    quotechar = '"'
    doublequote = True
    skipinitialspace = False
    lineterminator = '\n'
    quoting = csv.QUOTE_MINIMAL
csv.register_dialect("excel_unix", excel_unix)

testdata = range(10)

for dialect in ["excel","excel_unix","excel_mac"]:
	try:
		print '\n Testing dialect "%s"' % dialect
		test = file('test.csv','w')
		writer = csv.writer(test,dialect=dialect)
		for i in range(3):
			writer.writerow(testdata)
		test.close()
		
		test = file('test.csv','r')
		reader = csv.reader(test,dialect=dialect)
		for row in reader:
			print row
	except:
		traceback.print_exc()

#Results
"""
 Testing dialect "excel"
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

 Testing dialect "excel_unix"
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

 Testing dialect "excel_mac"
Traceback (most recent call last):
  File "/Users/lmeyn/Desktop/testcsv.py", line 36, in ?
    print row
Error: newline inside string
"""
msg17808 - (view) Author: Larry Meyn (lmeyn) Date: 2003-08-21 15:44
Logged In: YES 
user_id=28540

If I open files using the universal, 'U', mode this is not a problem.  
Too bad the 'U' mode is not the default. (I know, there is a lot of 
*nix python code out there that opens binary files as text, and it 
would all break if 'U' were the default.)
msg17809 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2003-08-21 16:01
Logged In: YES 
user_id=44345

We've been discussing this bug on the csv mailing list.  I suspect
we will make a change to U mode for 2.3.1 and 2.4, though we
have yet to test the proposed change.

Skip
msg17810 - (view) Author: Bill Bumgarner (bbum) Date: 2003-08-21 18:02
Logged In: YES 
user_id=103811

Why doesn't the csv module use the lineterminator for reading 
files?  As it is, the lineterminator appears to be used only for the 
writing of rows to a file.

This, of course, means that csv will quite happily write a file that it 
cannot subsequently read with the same dialect.
msg17811 - (view) Author: Andrew McNamara (andrewmcnamara) * (Python committer) Date: 2005-01-13 04:10
Logged In: YES 
user_id=698599

I'm going to close this issue - we will attempt to improve the documentation, 
so as to bring this issue to user's awareness, but the supplied iterator 
needs to return "lines" - on a file using \r, this means using universal 
newline mode.
History
Date User Action Args
2022-04-10 16:10:40adminsetgithub: 39074
2003-08-15 21:16:38lmeyncreate