如何在csv中循环并能够返回一步或逐行进行



我正在为用户查找时间。假设他们输入13(分钟(,我的代码滚动csv,找到时间列中有13的每一行。然后它一次打印出一行。我不知道如何允许用户选择重新访问前一步?我的代码目前只是颠倒csv的顺序,从底部开始,即使行不是13分钟选择的行。

我是个新手,所以请尽量简单地解释。。感谢

请参阅代码:

def time():
while True:
find = input("Please enter a time in minutes(rounded)n"
"> ")
if len(find) < 1:
continue
else:
break

print("Matches will appear belown"
"If no matches were maden"
"You will return back to the previous menu.n"
"")
count = -1
with open("work_log1.csv", 'r') as fp:
reader = csv.DictReader(fp, delimiter=',')
for row in reader:
count+=1
if find == row["time"]:
for key, value in row.items(): # This part iterates through csv dict with no problems
print(key,':',value)
changee = input("make change? ")
if changee == "back":                    # ISSUE HERE******
for row in reversed(list(reader)):   # I'm trying to use this to reverse the order of whats been printed
for key, value in row.items():   # Unfortunately it doesnt start from the last item, it starts from
print(key,':',value)         # The bottom of the csv. Once I find out how to iterate through it
# Properly, then I can apply my changes
make_change = input("make change?  or go back")
if make_change == 'y':
new_change = input("input new data: ")
fp = pd.read_csv("work_log1.csv")
fp.set_value(count, "time", new_change) # This part makes the changes to the row i'm currently on
fp.to_csv("work_log1.csv", index=False)


print("")

您总是可以有一个保留最后一行n的列表,这样您就可以在读取新行history.pop(0)和'history.append(last_line('后使用此列表返回

或者,您可以使用stream.seek函数包装此逻辑

相关内容

最新更新