简体   繁体   English

每行要文件包含多个值-python

[英]multiple values per row to file - python

I'm trying to store scores per user to a file in python and I'm not being able to. 我正在尝试将每个用户的分数存储到python中的文件中,而我却无法这样做。

I have two lists, names[] & points[] . 我有两个列表, names[]points[] Everytime the program runs it appends user and score, but when it's run again, the score in the file gets overwritten rather than added to that user. 程序每次运行时,它都会附加用户和得分,但是再次运行时,文件中的得分将被覆盖,而不是添加到该用户。

def quiz():

names=[]
points=[]
name=input("What is your name?")
for x in range (0,1)
    pnt=0  
    num1=3
    num2=4
    print("Whats", num1, '+', num2)
    answer = num1+num2
 userAns=int(input())
 if userAns==answer:
    print("Excellent")
    pnt = pnt+1
 else:
    print("wrong") 
    print("Well done", name, "you scored", point, "/10")
    points.append(point)
    names.append(name)
    class_name = class_name + ".csv"   
    c=open(class_name , 'a+') ##This part forward it prints to another row rather then appending score in the same row
    mw = csv.writer(c)
    rows = zip(names,points)
    mw.writerows(rows)
    c.close()

I need to print the user and then multiple scores alongside it, any help would be appreciated 我需要打印用户,然后打印多个分数,任何帮助将不胜感激

Hmm, your code cannot be run, and it does not look like what it does what it should be doing even other than the part about appending values to the same row. 嗯,您的代码无法运行,除了将值附加到同一行之外,它看起来也不像它应该做什么。 I am confused as to what the actual requirements are for what you are trying to do, but to append values to the same row (assuming each user gets his own file): 我对您要执行的操作的实际要求感到困惑,但是将值附加到同一行(假设每个用户都有自己的文件):

c=open(class_name , 'a+') 

can be changed to 可以更改为

c=open(class_name , 'rb+')  # csv are considered as binary files
c.seek(-2,2) # go to \r\n
mv.writerows(   #... write now

Alternatively, if all users and scores are in the same file (which seems to be what you want, based on your description), you can simply replace the contents of the current file, so just write to it normally and replace the existing contents 或者,如果所有用户和乐谱都在同一个文件中(根据您的描述,这似乎是您想要的),则可以简单地替换当前文件的内容,因此只需正常写入并替换现有内容即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM