我是Python的新手,我正在编写一个程序
我在第4点挣扎,我不断收到错误,我的数据是列表而不是字符串。
我的文本文件有6个以逗号分隔的列:user_name后跟6个数字,即 joes bloggs,1,2,3,4,5,6
这是我的代码:
fname = input('First name? ')
fname = fname.lower()
lname = input('Last name? ')
lname = lname.lower()
name = (fname + ' ' + lname)
score=8
with open('test.txt','r+') as df:
data=[]
for row in df:
data.append(row.strip().split(','))
#for checking
print(data)
for row in data:
if name == row[0]:
#for checking
print(row)
row[3]=row[2]
row[2]=row[1]
row[1]=score
second=row[2]
third=row[3]
scores = [float(score),float(second),float(third)]
row[5]=float(int((float(score)+float(second)+float(third))/ 0.03)/100)
row[4]=max(scores)
#for checking
print(row)
df.seek(0)
for row in data:
for item in row:
df.write(item + ',')
df.write(row +'\n')
df.truncate()
df.close()
答案 0 :(得分:0)
for row in data:
df.write(",".join(row) +'\n')