Python:如何搜索和写入CSV文件?

时间:2016-02-20 17:51:18

标签: python csv input

我正在尝试创建一个程序,要求用户输入一些信息,然后在csv文件中搜索该信息。一旦找到我想要的信息,然后在下一列中写入更多数据(也由用户输入),但是在同一行上。

如果不清楚,我会在下面举一个例子。

Name = input("What is your name?") # I want to search for this in my CSV file.
Age = input("What is Your age?") # I want this to be input into the next column along but still on the same row of the CSV file.
with open("Test.csv", "r") as csvfile:
csvfilereader = csv.reader(csvfile)
for row in csvfilereader:
    for field in row:
        if field == Name:
            print(row) # I can find and print data along the row, but I am not sure how to input data in the same row.

非常感谢任何帮助!

当前的CSV文件:

Name    Speed
John
Jamie
Jake

我希望它看起来像:

Name    Speed
John    20 #Assuming these values are entered by the user.
Jamie   15
Jake    25

0 个答案:

没有答案