TypeError: a bytes-like object is required, not 'str' when trying to write in csv file
TypeError: a bytes-like object is required, not 'str' when trying to write in csv file
我必须将 Python 2 中编写的以下代码转换为 Python3:
with open('C:/path_to_csv_file/fold1_1.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row, timeseq in izip(fold1, fold1_t):
spamwriter.writerow([unicode(s).encode("utf-8") +'#{}'.format(t) for s, t in izip(row, timeseq)])
我的Python3代码如下:
with open('C:/Users/path_to_csv_file/fold1_1.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row, timeseq in zip(fold1, fold1_t):
for s, t in zip(row, timeseq):
bytes_var = s.encode('utf-8') +'#{}'.format(t).encode('utf-8')
spamwriter.writerow(bytes_var)
fold1 包含:
fold1 = ['¢©§','¢©§','¢©§']
fold1_t 包含:
fold1_t =[[0, 15, 173999],[0, 457325, 306],[0, 62954, 432]]
当我尝试执行上面的代码时,出现以下错误:
TypeError: a bytes-like object is required, not 'str'
变量 bytes_var 是字节类型,所以通常它应该可以工作。
我是不是在从 Python2 到 Python3 的转换中做错了什么来得到那个错误?
谢谢。
您需要将 wb
更改为 w
。此外,在 python 3 中,您可以通过 直接 将表达式传递到字符串的花括号中来格式化字符串,因此
s.encode('utf-8') +'#{}'.format(t).encode('utf-8')
可以
f'{s}#{t}'.encode('utf-8')
一共:
import csv
fold1 = ['¢©§','¢©§','¢©§']
fold1_t =[[0, 15, 173999], [0, 457325, 306], [0, 62954, 432]]
with open('C:/path_to_csv_file/fold1_1.csv', 'w') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row, timeseq in zip(fold1, fold1_t):
for s, t in zip(row, timeseq):
bytes_var = s.encode('utf-8') + f'#{t}'.encode('utf-8')
spamwriter.writerow(bytes_var)
csv
模块是 Python2 -> Python3 转换不是很简单的模块之一。基础文件应以文本形式打开,不再以二进制文件形式打开,但行尾为空。
所以我会写:
with open('C:/path_to_csv_file/fold1_1.csv', 'w', newline='', encoding='utf-8') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row, timeseq in izip(fold1, fold1_t):
spamwriter.writerow([s +'#{}'.format(t) for s, t in izip(row, timeseq)])
当前错误是由于csv模块发送的是unicode字符串,而底层文件需要字节。
我必须将 Python 2 中编写的以下代码转换为 Python3:
with open('C:/path_to_csv_file/fold1_1.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row, timeseq in izip(fold1, fold1_t):
spamwriter.writerow([unicode(s).encode("utf-8") +'#{}'.format(t) for s, t in izip(row, timeseq)])
我的Python3代码如下:
with open('C:/Users/path_to_csv_file/fold1_1.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row, timeseq in zip(fold1, fold1_t):
for s, t in zip(row, timeseq):
bytes_var = s.encode('utf-8') +'#{}'.format(t).encode('utf-8')
spamwriter.writerow(bytes_var)
fold1 包含:
fold1 = ['¢©§','¢©§','¢©§']
fold1_t 包含:
fold1_t =[[0, 15, 173999],[0, 457325, 306],[0, 62954, 432]]
当我尝试执行上面的代码时,出现以下错误:
TypeError: a bytes-like object is required, not 'str'
变量 bytes_var 是字节类型,所以通常它应该可以工作。 我是不是在从 Python2 到 Python3 的转换中做错了什么来得到那个错误? 谢谢。
您需要将 wb
更改为 w
。此外,在 python 3 中,您可以通过 直接 将表达式传递到字符串的花括号中来格式化字符串,因此
s.encode('utf-8') +'#{}'.format(t).encode('utf-8')
可以
f'{s}#{t}'.encode('utf-8')
一共:
import csv
fold1 = ['¢©§','¢©§','¢©§']
fold1_t =[[0, 15, 173999], [0, 457325, 306], [0, 62954, 432]]
with open('C:/path_to_csv_file/fold1_1.csv', 'w') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row, timeseq in zip(fold1, fold1_t):
for s, t in zip(row, timeseq):
bytes_var = s.encode('utf-8') + f'#{t}'.encode('utf-8')
spamwriter.writerow(bytes_var)
csv
模块是 Python2 -> Python3 转换不是很简单的模块之一。基础文件应以文本形式打开,不再以二进制文件形式打开,但行尾为空。
所以我会写:
with open('C:/path_to_csv_file/fold1_1.csv', 'w', newline='', encoding='utf-8') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row, timeseq in izip(fold1, fold1_t):
spamwriter.writerow([s +'#{}'.format(t) for s, t in izip(row, timeseq)])
当前错误是由于csv模块发送的是unicode字符串,而底层文件需要字节。