Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按行搜索和修改CSV文件,只需一个或两个元素,不一定是该行中的所有元素 问题_Python_Python 3.x_File_Csv_Python Requests - Fatal编程技术网

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

Python 按行搜索和修改CSV文件,只需一个或两个元素,不一定是该行中的所有元素 问题,python,python-3.x,file,csv,python-requests,Python,Python 3.x,File,Csv,Python Requests,我想知道搜索和修改2D列表中某个元素的正确算法/方法。修改后的2D列表将稍后写入csv文件。以下是我的脚本: 代码 预期产量 提出的搜索算法,我在网上找到的,但仍然缺乏修改,以合成我的代码 for row in pendingFile: if search == row[0]: print(row) 我还是新手,我已经在谷歌和youtube上冲浪两天了,但在这个问题上还没有足够的突破。 我真的需要帮助,我是一名学生,我们的任务是创建GUI POS Python 3 Tk

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

代码 预期产量 提出的搜索算法,我在网上找到的,但仍然缺乏修改,以合成我的代码

for row in pendingFile:
    if search == row[0]:
        print(row)
我还是新手,我已经在谷歌和youtube上冲浪两天了,但在这个问题上还没有足够的突破。 我真的需要帮助,我是一名学生,我们的任务是创建GUI POS Python 3 Tkinter作为最终项目个人


我目前的编程问题对我来说可能是个大问题,但我真诚地感谢您提供的慷慨帮助。

您发布的三行代码片段是正确的。 您可能会发现使用字典阅读器更方便:。 阅读完文件后,您会在内存中看到一个
dict
s列表,而不是一个神秘的
0
,您可以为每次访问使用一个符号列名,如下所示

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

做了几次这样的更改后,您可能希望使用DictWriter保存更新的行。

您发布的三行片段是正确的。 您可能会发现使用字典阅读器更方便:。 阅读完文件后,您会在内存中看到一个
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():
打开(“库存列表.csv”,“r”)作为我的库存列表:
myFile=csv.reader(my_csv,delimiter=“,”)
全球数据清单
对于myFile中的列,dataInventoryList=[[col[0],col[1],col[2],col[3],col[4],eval(col[5])]
def dataWrite():
打开(“库存清单.csv”,“w+”)作为我的库存清单csv:
myFile=csv.writer(my_csv,分隔符=',',行终止符='\n')
myFile.writerows(dataInventoryList)
def main():
尽管如此:
发现=错误
数据读取()
打印(“==================================================================================================================================”)
打印(“更新前;”)
对于范围内的i(len(dataInventoryList)):
对于范围(6)内的j:
打印(str(dataInventoryList[i][j])+“\t”,end=”“)
打印(“”)
搜索=输入(“输入产品代码:”)
数量=整数(输入(“请输入数量:”)
choice=int(输入(“添加库存[1]拉动堆栈[2]\n提示:))
对于范围内的i(len(dataInventoryList)):
如果搜索==dataInventoryList[i][0]:
找到=真
如果选项==1:
数据清单[i][5]+=数量
打破
elif选项==2:
如果dataInventoryList[i][5]==0:
打印(“请求的订单缺货!\n”)
打破
elif quantity>dataInventoryList[i][5]:
打印(“请求的订单超出范围!\n”)
打破
elif我设法构建的我所寻找的数量:
def dataRead():
打开(“库存列表.csv”,“r”)作为我的库存列表:
myFile=csv.reader(my_csv,delimiter=“,”)
全球数据清单
对于myFile中的列,dataInventoryList=[[col[0],col[1],col[2],col[3],col[4],eval(col[5])]
def dataWrite():
打开(“库存清单.csv”,“w+”)作为我的库存清单csv:
myFile=csv.writer(my_csv,分隔符=',',行终止符='\n')
myFile.writerows(dataInventoryList)
def main():
尽管如此:
发现=错误
数据读取()
打印(“==================================================================================================================================”)
打印(“更新前;”)
对于范围内的i(len(dataInventoryList)):
对于范围(6)内的j:
打印(str(dataInventoryList[i][j])+“\t”,end=”“)
打印(“”)
搜索=输入(“输入产品代码:”)
数量=整数(输入(“请输入数量:”)
choice=int(输入(“添加库存[1]拉动堆栈[2]\n提示:))
对于范围内的i(len(dataInventoryList)):
如果搜索==dataInventoryList[i][0]:
找到=真
如果选项==1:
数据清单[i][5]+=数量
打破
elif选项==2:
如果dataInventoryList[i][5]==0:
打印(“请求的订单缺货!\n”)
打破
elif quantity>dataInventoryList[i][5]:
打印(“请求的订单超出范围!\n”)
打破

elif数量已解决!对于my_csv中的列:dataInventoryList.append([col[0],col[1]])已解决!对于my_csv中的列:dataInventoryList.append([col[0],col[1]])
for row in rows:
    if search == row['name']:
        print(f'Found {search}, converting to capitals.')
        row['name'] = search.upper()
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()