TypeError:需要类似字节的对象,而不是“str”:获取时出错

2024-09-30 01:25:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写入输出文件,但出现以下错误:

TypeError: a bytes-like object is required, not 'str'

有谁能推荐我吗?在

import csv

fin = open('another_summary_table.csv')
words = ["PCB","Switch","Socket","Red-Green LED","Solar Panel","Battery", "Load wire"]
found = {}

wr = csv.writer(open("boop-1.csv", "wb"))
fieldnames = ['PartsReplaced']
writer = csv.DictWriter(fin, fieldnames=fieldnames)
for line in fin:
    split_line=line.split(',')
    str1=split_line[2] # Whatever columns
    PartsReplaced=split_line[2] # Whatever columns

    for w in words:
        if w in str1:
            found[w]=found.get(w,0)+1

            break
wr.writerows(found)

Tags: csvinforlineopenwrwritersplit

热门问题