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

时间:2017-04-25 11:50:07

标签: 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

1 个答案:

答案 0 :(得分:0)

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

array=[]    

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