如何附加到CSV文件?

时间:2021-12-29 06:21:28

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

使用Python附加CSV文件,我每隔一行获取一次数据。我该如何解决?

import csv

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

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

C:\test.csv when opened looks like this:

打开时C:\ test.csv如下所示:

1,2

3,4

1,2

3,4

2 个解决方案

#1


37  

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 \r\n as expected but then the Windows C runtime kicks in and replaces the \n with \r\n so that you have \r\r\n between rows. When you "open" the csv file with Excel it becomes confused

不管你信不信,CSV都是二进制格式。 csv模块正在按照预期将错误命名的“lineterminator(应该是”rowseparator“)写为\ r \ n,但随后Windows C运行时启动并用\ r \ n替换\ n,以便您有\ r \行之间的r \ n。当你用Excel“打开”csv文件时,它会变得混乱

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).

无论您是否在Windows上运行,始终以二进制模式('rb','wb','ab')打开CSV文件。这样,你甚至可以在* x盒子上获得预期的rowseparator(CR LF),你的代码将是可移植的,并且你的数据中嵌入的任何换行都不会被改成别的东西(写作时)或导致戏剧(输入时) ,当然提供它们是正确引用的)。

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.

(1)不要将数据放在根目录(C:\)中。 Windows在20世纪80年代继承了MS-DOS的分层文件系统。用它。

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

(2)如果必须在代码中嵌入硬连线文件名,请使用原始字符串r“c:\ test.csv”...如果你有“c:\ test.csv”,'\ t'将被解释为一个TAB角色;与\ r和\ n类似的问题

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

(3)Python手册中的示例与简洁的代码相比更加简洁。

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.

然后,当您完成后,您可以使用f,以便您可以执行f.close()以确保将文件内容刷新到磁盘。更好的是:阅读新的声明。

#2


2  

I have encountered a similar problem with appending an already created csv file, while running on windows.

我在Windows上运行时遇到类似的问题,附加已经创建的csv文件。

As in this case writing and appending in "binary" mode avoids adding extra line to each rows written or appended by using the python script. Therefore;

在这种情况下,以“二进制”模式写入和追加可以避免使用python脚本为每个写入或追加的行添加额外的行。因此;

w = csv.writer(open(Fn,'ab'),dialect='excel')

#1


37  

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 \r\n as expected but then the Windows C runtime kicks in and replaces the \n with \r\n so that you have \r\r\n between rows. When you "open" the csv file with Excel it becomes confused

不管你信不信,CSV都是二进制格式。 csv模块正在按照预期将错误命名的“lineterminator(应该是”rowseparator“)写为\ r \ n,但随后Windows C运行时启动并用\ r \ n替换\ n,以便您有\ r \行之间的r \ n。当你用Excel“打开”csv文件时,它会变得混乱

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).

无论您是否在Windows上运行,始终以二进制模式('rb','wb','ab')打开CSV文件。这样,你甚至可以在* x盒子上获得预期的rowseparator(CR LF),你的代码将是可移植的,并且你的数据中嵌入的任何换行都不会被改成别的东西(写作时)或导致戏剧(输入时) ,当然提供它们是正确引用的)。

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.

(1)不要将数据放在根目录(C:\)中。 Windows在20世纪80年代继承了MS-DOS的分层文件系统。用它。

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

(2)如果必须在代码中嵌入硬连线文件名,请使用原始字符串r“c:\ test.csv”...如果你有“c:\ test.csv”,'\ t'将被解释为一个TAB角色;与\ r和\ n类似的问题

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

(3)Python手册中的示例与简洁的代码相比更加简洁。

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.

然后,当您完成后,您可以使用f,以便您可以执行f.close()以确保将文件内容刷新到磁盘。更好的是:阅读新的声明。

#2


2  

I have encountered a similar problem with appending an already created csv file, while running on windows.

我在Windows上运行时遇到类似的问题,附加已经创建的csv文件。

As in this case writing and appending in "binary" mode avoids adding extra line to each rows written or appended by using the python script. Therefore;

在这种情况下,以“二进制”模式写入和追加可以避免使用python脚本为每个写入或追加的行添加额外的行。因此;

w = csv.writer(open(Fn,'ab'),dialect='excel')