小编Jor*_*dan的帖子

写入文本文件错误 - 必须是 str,而不是列表

我在编写一些代码时遇到了问题。基本上,当我运行代码时,我输入了一个 8 位数字,它应该扫描 CSV 文件以查看该数字是否在文件中。如果是,则应将该行写入文本文件。但是,当我运行它并输入一个数字时,我得到了这个:

TypeError: must be str, not list
Run Code Online (Sandbox Code Playgroud)

(编辑)即使是固定的,输出也是:

<_io.TextIOWrapper name='receipt.txt' mode='a' encoding='cp1252'> 
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

import csv
import sys
import re

addItem = ""
gtinNum = ""
quantity = 0
totalPrice = 0

receipt = open("receipt.txt", "r+")
f = open("ChocolateCSV.csv", "rt")

def scanGTIN():
    rows = re.split('\n', f.read())

    for index, row in enumerate(rows):
        global cells
        cells = row.split(',')
        if gtinNum in cells:
            receipt.write(cells)

def gtinQuestion():
    global gtinNum
    gtinNum = input("Please enter the GTIN-8 Code of the product you would …
Run Code Online (Sandbox Code Playgroud)

python csv text writing

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

csv ×1

python ×1

text ×1

writing ×1