在Windows 8上使用Python 3.3,在写入CSV文件时,我收到错误TypeError: 'str' does not support the buffer interface
和"wb"
标志。但是,当只使用"w"
标志时,我没有错误,但是每一行都用一个空行隔开!
代码
test_file_object = csv.reader( open("./files/test.csv", 'r') )
next(test_file_object )
with open("./files/forest.csv", 'wb') as myfile:
open_file_object = csv.writer( open("./files/forest.csv", 'wb') )
i = 0
for row in test_file_object:
row.insert(0, output[i].astype(np.uint8))
open_file_object.writerow(row)
i += 1
错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-121-8cbb94f602a8> in <module>()
8 for row in test_file_object:
9 row.insert(0, output[i].astype(np.uint8))
---> 10 open_file_object.writerow(row)
11 i += 1
TypeError: 'str' does not support the buffer interface
阅读时,我似乎无法使用"rb"
标志,因此在尝试忽略第一行(标题)时会出现错误iterator should return strings, not bytes
。
代码
csv_file_object = csv.reader(open('files/train.csv', 'rb'))
header = next(csv_file_object)
train_data = []
for row in csv_file_object:
train_data.append(row)
train_data = np.array(train_data)
错误
Error Traceback (most recent call last)
<ipython-input-10-8b13d1956432> in <module>()
1 csv_file_object = csv.reader(open('files/train.csv', 'rb'))
----> 2 header = next(csv_file_object)
3 train_data = []
4 for row in csv_file_object:
5 train_data.append(row)
Error: iterator should return strings, not bytes (did you open the file in text mode?)
答案 0 :(得分:25)
对于Python 2,'wb'
模式是正常的。但是,它在Python 3中是错误的。在Python 3中,csv阅读器需要字符串,而不是字节。这样,您必须在文本模式下打开它。但是,在阅读内容时,不得解释\n
。这样,您必须在打开文件时传递newline=''
:
with open("./files/forest.csv", newline='') as input_file \
open('something_else.csv', 'w', newline='') as output_file:
writer = csv.writer(output_file)
...
如果文件不是纯ASCII,您还应考虑添加encoding=...
参数。
答案 1 :(得分:2)
嗨,这可能会对你有所帮助。
第一个问题,
更改
with open("./files/forest.csv", 'wb') as myfile:
open_file_object = csv.writer( open("./files/forest.csv", 'wb') )
到
with open("./files/forest.csv", 'w+') as myfile:
open_file_object = csv.writer( open("./files/forest.csv", 'w+') )
第二个问题:
同样的事情,除了改为r +
如果这不起作用,您可以随时使用它来删除创建后的所有空白行。
for row in csv:
if row or any(row) or any(field.strip() for field in row):
myfile.writerow(row)
另外,一点教训。 “rb”代表读取字节,基本上认为它只是读取整数。我不确定你的csv的内容是什么;但是,csv中必须有字符串。
这有助于将来参考。
参数模式指向以下列之一开头的字符串 序列(附加字符可以遵循这些序列。):
``r'' Open text file for reading. The stream is positioned at the
beginning of the file.
``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.
``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
``w+'' Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.
``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.
``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.