Итак, в основном я нашел код, который может импортировать CSV-файл в 2D-список, но я не могу понять, как циклы «for» работают внутри списка.То же самое относится и к циклу for внутри оператора if.
Вот мой код:
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 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], eval(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()
~Inventory_List.csv
CS001,Pieces,Hardie Flex,Construction Materials,5,100
CS002,Pieces,Hammer,Construction Materials,4,100
CS003,Pieces,Mallet,Construction Materials,7,100