Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

python - How to append to a CSV file?

Using Python to append CSV file, I get data every other row. How do I fix?

import csv

LL = [(1,2),(3,4)]

Fn = ("C:Test.csv")
w = csv.writer(open(Fn,'a'), dialect='excel')
w.writerows(LL)

C:est.csv when opened looks like this:

1,2

3,4

1,2

3,4
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Appending is irrelevant to the problem; notice that the first two rows (those from the original file) are also double-spaced.

The real problem is that you have opened your file in text mode.

CSV is a binary format, believe it or not. The csv module is writing the misleadingly-named "lineterminator (should be "rowseparator") as as expected but then the Windows C runtime kicks in and replaces the with so that you have between rows. When you "open" the csv file with Excel it becomes confused

Always open your CSV files in binary mode ('rb', 'wb', 'ab'), whether you are operating on Windows or not. That way, you will get the expected rowseparator (CR LF) even on *x boxes, your code will be portable, and any linefeeds embedded in your data won't be changed into something else (on writing) or cause dramas (on input, provided of course they're quoted properly).

Other problems:

(1) Don't put your data in your root directory (C:). Windows inherited a hierarchical file system from MS-DOS in the 1980s. Use it.

(2) If you must embed hard-wired filenames in your code, use raw strings r"c:est.csv" ... if you had "c:est.csv" the '' would be interpreted as a TAB character; similar problems with and

(3) The examples in the Python manual are aligned more towards brevity than robust code.

Don't do this:

w = csv.writer(open('foo.csv', 'wb'))

Do this:

f = open('foo.csv', 'wb')
w = csv.writer(f)

Then when you are finished, you have f available so that you can do f.close() to ensure that your file contents are flushed to disk. Even better: read up on the new with statement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...