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