将记事本中的行转换为数组

2024-10-03 15:24:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个程序,它接受一个代码并在产品数据库记事本中搜索一行,一旦找到它,它将以某种方式对数组进行排序,当与其他行组合时,它看起来就像一个收据。你知道吗

for line in productDatabase:
    if requestedProduct in line:
        foundVariable = True

这段代码在记事本中的行中搜索代码。我需要弄清楚如何将特定的行转换成数组,以便以后使用。你知道吗

这是全部代码。你知道吗

exitDecision = int(input("Are you done? 1 = yes, 2 = no: "))

while exitDecision == 2:
    requestedProduct = input("Enter the GTIN-8 code of the product you want to buy: ")
    requestedProductQuantity = input("Enter the quantity of the product: ")
    productDatabase = open("productDatabase.txt","r")
    foundVariable = False
    for line in productDatabase:
        if requestedProduct in line:
            foundVariable = True


    if foundVariable == True:
        #Code to sort the line goes here.
    else:   
        #Code to sort the line if the product isn't found goes here. 

这是记事本的数据。你知道吗

29471829,milk,0.99
94726596,bread,1.20
26562636,paracetamol,2.99
45614621,Coca Cola,0.99
38192471,Butter,0.80

Tags: theto代码intrueforinputif
1条回答
网友
1楼 · 发布于 2024-10-03 15:24:30

当您检查产品是否与if requestedProduct in line:一致时,line包含您想要的行,您只需拆分它

array=[]    

for line in productDatabase:
    if requestedProduct in line:
      array.append(line.split(','))

相关问题 更多 >