逐行搜索和修改CSV文件,只有一个或两个元素,不一定是该行中的所有元素



问题

我想知道正确的算法/方法在搜索和修改我的2D列表的某些元素时。修改后的2D列表将稍后写入CSV文件。以下是我的脚本:

代码

import csv
search = input("Enter name to find: ")
replace = input("Enter name replacement: ")
pendingFile = csv.reader(open("pendingLists.csv", "r"), delimiter=",")        
#Problem 1 -> Searching and Modifying algorithm here...  
#Problem 2 -> Writing Algorithm here where the row is modified...

预期输出

>>>Enter name to find: Mark
>>>Enter name replacement: Jave
Old listfile
~listFile.csv
Job,PB01
Mark,NU01
Jumeirah,SC01
Same modified listfile
~listFile.csv
Job,PB01
Jave,NU01
Jumeirah,SC01

我在网络上发现的搜索算法,但仍缺乏修改要在我的代码上合成的

for row in pendingFile:
    if search == row[0]:
        print(row)

我仍然是新手,我已经在Google和YouTube上浏览了2天,但对这个问题的突破还不够。我真的需要帮助,我是一名学生,我们的任务是创建GUI POS Python 3 Tkinter作为最终项目个人。

我当前的编程问题对我来说可能很大,但我真的感谢您可能提供的慷慨帮助。

您发布的三行片段是正确的。您可能会发现使用字典阅读器更方便:https://docs.python.org/3/library/csv.html#csv.dictreader。然后,读取您在内存中使用dict s列表的文件,而不是隐秘的0,您可以使用每个访问的符号列名称,如下所示。

for row in rows:
    if search == row['name']:
        print(f'Found {search}, converting to capitals.')
        row['name'] = search.upper()

进行了几个这样的更改,您可能想使用dictwriter保存更新的行。

我设法构造了我正在寻找的东西:

def dataRead():
    with open("Inventory_List.csv", "r") as my_csv:
        myFile = csv.reader(my_csv, delimiter=",")
        global dataInventoryList
        dataInventoryList = [[col[0], col[1], col[2], col[3], col[4], eval(col[5])] for col in myFile]

def dataWrite():
    with open("Inventory_List.csv", "w+") as my_csv:
        myFile = csv.writer(my_csv, delimiter=',', lineterminator='n')
        myFile.writerows(dataInventoryList)
def main():
    while True:
        found = False
        dataRead()
        print("==========================================================================")
        print("Before update;")
        for i in range(len(dataInventoryList)):
            for j in range(6):
                print(str(dataInventoryList[i][j]) + "t", end="")
            print("")
        search = input("Enter product code: ")
        quantity = int(input("Please enter quantity: "))
        choice = int(input("Add stocks[1] Pull Stacks[2]nChoice: "))
        for i in range(len(dataInventoryList)):
            if search == dataInventoryList[i][0]:
                found = True
                if choice == 1:
                    dataInventoryList[i][5] += quantity
                    break
                elif choice == 2:
                    if dataInventoryList[i][5] == 0:
                        print("Requested order is out of stocks!n")
                        break
                    elif quantity > dataInventoryList[i][5]:
                        print("Requested order is out of range!n")
                        break
                    elif quantity <= dataInventoryList[i][5]:
                        dataInventoryList[i][5] -= quantity
                        break
        if found == False:
            print("Requested order was not found on the database!n")
        elif found == True:
            dataWrite()
        print("After update;")
        for i in range(len(dataInventoryList)):
            for j in range(6):
                print(str(dataInventoryList[i][j]) + "t", end="")
            print("")
main()

最新更新