如果你们能帮助我如何使用python中的csv模块使用该行内的唯一购买ID来读取csv文件中的行,我将不胜感激。 This is the format my csv file is in
这就是我写入csv文件的方式(如果有帮助的话)。
userNames = input("What are the full names of the people you are ordering tickets for? ")
localTime = time.asctime(time.localtime(time.time()) )
with open("Purchases.csv", "a") as f:
f.write(f"\n{userNames}, {ticketList[0]}, {ticketList[1]}, {ticketList[2]}, {ticketList[3]}, {totalCost}, {localTime}, {uniqueID}\n")
print(f"Fantasialand Ticket!")
在这里我需要帮助来读取具有唯一ID的行:
def searchingForFile():
iUniqueID = int(input("What is the Unique ID of the past purchase?")
with open("Purchases.csv", mode="rt", encoding='ascii') as f:
spamreader = csv.reader(f, delimiter=' ', quotechar='|')
for row in spamreader :
print(' '.join(row))
答案 0 :(得分:0)
唯一ID似乎是CVS中的最后一列:
const changeSelectValue = (state, action) => {
switch (action.id) {
case 'myFirstKey':
updatedState = {
...state,
myFirstKey: action.value
}
case 'mySecondKey':
updatedState = {
...state,
mySecondKey: action.value
}
break;
default:
break;
}}
在旁注:我强烈建议使用def searchingForFile():
iUniqueID = int(input("What is the Unique ID of the past purchase?")
with open("Purchases.csv", mode="rt", encoding='ascii') as f:
spamreader = csv.reader(f, delimiter=' ', quotechar='|')
is_first_row = True
for row in spamreader :
print(' '.join(row))
# if you have a header row
if is_first_row:
# skip if header
is_first_row = False
else:
uid = int(row[-1])
if uid == iUniqueID:
return row
return None
(或csv.DictWriter)来编写CSV文件。它将进行转义,正确的方言等操作。
答案 1 :(得分:0)
您可以简单地检查行中是否包含该元素,
def searchingForFile():
iUniqueID = int(input("What is the Unique ID of the past purchase?")
with open("Purchases.csv", mode="rt", encoding='ascii') as f:
spamreader = csv.reader(f, delimiter=' ', quotechar='|')
for row in spamreader :
if iUniqueID in row:
print(' '.join(row))
或检查特定单元格中的值:
def searchingForFile():
iUniqueID = int(input("What is the Unique ID of the past purchase?")
with open("Purchases.csv", mode="rt", encoding='ascii') as f:
spamreader = csv.reader(f, delimiter=' ', quotechar='|')
for row in spamreader :
if iUniqueID == row[-1]:
print(' '.join(row))