def WorkLoad():
readWork=open("Todays_Work.txt","r")
for line in readWork.readlines():
    WorkLine = line.split()
    Order_No = WorkLine[0]
    Deliver_Address = WorkLine[1]
    Bay_Collection = WorkLine[2]
    Stock = WorkLine[3]
print(WorkLine[0],"\n",WorkLine[1],"\n",WorkLine[2],"\n",WorkLine[3])
print(WorkLine)


我目前从这里开始,但是它只打印出文本文件的最后一行。

最佳答案

尝试下面的代码,这将以字典的形式给您集体的结果。

from collections import OrderedDict

result = OrderedDict()  #creating a Ordered dictionary

#setting dictionary elements
result["Order_No"] = []
result["Deliver_Address"] = []
result["Bay_Collection"] = []
result["Stock"] = []

def WorkLoad(result):
    readWork=open("Todays_Work.txt","r")
    for line in readWork.readlines():
        WorkLine = line.split()
        result["Order_No"].append(WorkLine[0])
        result["Deliver_Address"],append(WorkLine[1])
        result["Bay_Collection"].append(WorkLine[2])
        result["Stock"].append(WorkLine[3])
    return result

data = Workload(result)  #calling the workload function
print(data)  #printing the data

10-05 23:17