Python 3.3 CSV.Writer写入额外的空白行 [英] Python 3.3 CSV.Writer writes extra blank rows
问题描述
在Windows 8上使用Python 3.3时,当写入CSV文件时,我收到错误 TypeError:'str'不支持缓冲区接口
和使用wb
标志。但是,当只使用w
标志时,我没有错误,但是每一行都被空白行分隔!
问题写作
代码
test_file_object = csv.reader(open(./ files / test.csv,'r'))
next(test_file_object)
with open forest.csv,'wb')as myfile:
open_file_object = csv.writer(open(./ files / forest.csv,'wb'))
i = 0
row in test_file_object:
row.insert(0,output [i] .astype(np.uint8))
open_file_object.writerow(row)
i + = 1
错误
---------------------------------------------- -----------------------------
TypeError Traceback(最近一次调用)
< ipython-input -121-8cbb94f602a8> in< module>()
8对于test_file_object中的行:
9 row.insert(0,output [i] .astype(np.uint8))
---& 10 open_file_object.writerow(row)
11 i + = 1
TypeError:'str'不支持缓冲区接口
问题阅读
rb
标志,所以它会给错误迭代器应该返回字符串,而不是字节
header = next(csv_file_object)
train_data = []
for csv_file_object:
train_data.append(row)
train_data = np.array(train_data)
错误
错误回溯(最近一次调用)
< 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对于csv_file_object中的行:
5 train_data.append(row)
错误:字符串,而不是字节(你是否以文本模式打开文件?)
'wb'
模式适用于Python 2.但是,在Python 3中是错误的。在Python 3中,csv读取器需要字符串,而不是字节。这样,你必须在文本模式下打开它。但是,在读取内容时不能解释 \\\
。这样,打开文件时必须传递
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 = ...
参数。
Using Python 3.3 on Windows 8, when writing to a CSV file, I get the error TypeError: 'str' does not support the buffer interface
and "wb"
flag was used. However when only the "w"
flag was used, I get no errors, but every row is separated by a blank row!
Problem Writing
Code
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
Error
---------------------------------------------------------------------------
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
Problem Reading
When reading, I cant seem to use the "rb"
flags so it will give the error iterator should return strings, not bytes
when trying to ignore the first row (headers).
Code
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
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?)
The 'wb'
mode was OK for Python 2. However, it is wrong in Python 3. In Python 3, the csv reader needs strings, not bytes. This way, you have to open it in text mode. However, the \n
must not be interpreted when reading the content. This way, you have to pass newline=''
when opening the file:
with open("./files/forest.csv", newline='') as input_file \
open('something_else.csv', 'w', newline='') as output_file:
writer = csv.writer(output_file)
...
If the file is not pure ASCII, you should also consider to add the encoding=...
parameter.
这篇关于Python 3.3 CSV.Writer写入额外的空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!