看,我有一個文件,第一行有標題:[“創(chuàng)建日期”,“ID”,“Note”]我還有一個函數(shù)可以更改特定的行,但更改發(fā)生在第一點,也就是說,從日期開始。日期必須更改為當前日期,而不是手動更改
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變量增加一,但最終我得到了列表索引超出范圍的錯誤
python
range
函數(shù)可以接受兩個參數(shù)range(start, end)
,其中第一個參數(shù)是開始,所以只從1開始,而不是從0開始。