我在编写某些代码时遇到问题.基本上,当我运行代码时,我输入一个8位数字,它应该扫描CSV文件以查看数字是否在文件中.如果是这样,则应将该行写入文本文件.但是,当我运行它并输入一个数字时,我得到了:
TypeError: must be str, not list
(编辑)即使固定,输出为:
我的代码如下:
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 like to order:")
if gtinNum.isdigit() == False or len(gtinNum) != 8:
gtinQuestion()
elif gtinNum.isdigit() == True and len(gtinNum) == 8:
scanGTIN()
gtinQuestion()
任何帮助表示赞赏,谢谢.