我正在尝试创建一个代码来重新格式化 csv 文件并将其写入 csv 文件上的列表列表。代码不是很干净,但过去一直有效。但是现在我添加了一个新的第一部分,我删除了行并创建了中间文件,它给了我一个类型错误。我无法意识到我做错了什么,感谢您的帮助。
import os
import csv
outputfile = input("")
nodefile = input("")
inputfile =input("")
betweenfile= input("")
os.mknod(outputfile)
os.mknod(nodefile)
os.mknod(betweenfile)
with open(inputfile, newline='') as data:
reader = csv.reader(data, delimiter=',')
stack=[]
try:
for row in reader:
del row[0]
del row[3]
del row[3]
del row[3]
del row[3]
del row[3]
del row[4]
row[1],row[2]=row[2],row[1]
row[2],row[3]=row[3],row[2]
stack.append(row)
except:
IndexError
with open(betweenfile, 'w') as outputfile:
writer= csv.writer(outputfile, delimiter=',')
for row in stack:
writer.writerow(row)
with open(betweenfile, newline='') as data:
reader = csv.reader(data, delimiter=',')
listee=[]
for row in reader:
if row[3]=='1':
row[0],row[1]=row[1],row[0]
if row[3]=='3':
row[0],row[1]=row[1],row[0]
row.pop(3)
listee.append(row)
listee.pop(0)
for row in listee:
for i in range(len(row)):
if row[i]== '0':
listee.remove(row)
for row in listee:
for i in range(len(row)):
if row[i]== '0':
listee.remove(row)
print(type(listee))
with open(outputfile, 'w') as outputfile:
writer= csv.writer(outputfile, delimiter=',')
writer.writerow(['from', 'to' , 'weight'])
for row in listee:
writer.writerow(row)
错误信息是:
Traceback (most recent call last):
File "dataformater3.py", line 54, in <module>
with open(outputfile, 'w') as outputfile:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper