看,我有一个文件,标题在第一行:**[“Date of creation”,“ID”,“Note”]**我有一个函数,它可以更改特定的行,但更改是从第一个点开始,也就是从日期开始。必须将日期更改为当前日期,而不是手动更改
def editNote(path):
myList = []
print("Please enter the new details for each of the following: ")
# Create an empty array, write all the data from the file there
with open(path, 'r') as file:
reader = csv.reader(file)
for row in reader:
myList.append(row)
file.close()
# We display all notes by numbering, the user enters the number of the desired line
print("Please see the details to editing: ")
for i in range(len(myList)):
print("Row" + str(i) + " :" + str(myList[i]))
userInputToEdit = int(input("Which row would you like to change? Enter 1 - " + str(len(myList) - 1) + ": "))
# Changing data on a specific line
for i in range(len(myList[0])):
newDetails = input("Enter new data for " + str(myList[0][i + 1]) + ": ")
myList[userInputToEdit][i] = newDetails
# Show modified ARRAY
print("Please see the details of the new file below: ")
for i in range(len(myList)):
print("Row" + str(i) + " :" + str(myList[i]))
# If yes, then we make changes already in the file
changeCSV = input("Would you like to make the changes to the csv file? Y/N: ").lower()
if changeCSV == "y":
with open(path, 'w+', newline='') as file:
writer = csv.writer(file)
for i in range(len(myList)):
writer.writerow(myList[i])
file.close()
我试图将i变量增加1,但最终得到 list index out of range 错误
2条答案
按热度按时间ltqd579y1#
python
range
函数可以接受两个参数range(start, end)
,其中第一个参数是start,所以只需要从1开始而不是0。b1payxdu2#
要将日期更改为当前日期,您可以执行以下操作:
更改此:
这是:
重要注意事项包括:
请记住,每一行都是一个dict,因此您可以通过名称而不是索引来访问它。
你可以在一开始
import datetime