简体   繁体   English

如何从已拆分的.txt文件中获取多行并将其保存到不同的变量中

[英]How to get multiple lines from a .txt file that have been split and to save them into different variables

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)

I am currently started of with this but it only prints out the last line in the text file. 我目前从这里开始,但是它只打印出文本文件的最后一行。

Try below code this will give you collective result in the form of dictionary. 尝试下面的代码,这将以字典的形式给您集体的结果。

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

If you wish to print each line, you should add a print statement inside the loop. 如果希望打印每一行,则应在循环内添加一条打印语句。 Or save them to a later use. 或保存它们以备后用。 Probably you want something like this: 可能您想要这样的东西:

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

This prints each line during the loop and saves all the fields in the relative lists. 这将在循环期间打印每一行,并将所有字段保存在相对列表中。 I define the lists outside WorkLoad so they are available from other functions you may have. 我在WorkLoad之外定义列表,以便可以从其他功能中使用它们。 Hope this is helpful. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Spotipy 获取歌曲列表,然后将它们保存到 txt 文件? - How to get list of songs from Spotipy and then save them to a txt file? 如何从两个不同的txt文件中对数字进行排序然后将它们另存为一个txt文件 - How to sort numbers from two different txt files then save them as one txt file 从 2 个不同的 arrays 中的 txt 文件中拆分行的字符串 - Split lines's strings from a txt file in 2 different arrays 如何将txt文件拆分为多个文件,但不包含具有某些内容的行 - How to split a txt file into multiple files excluding lines with certain content 如何从txt文件中获取分隔行 - How to get seperated lines from txt file 尝试将txt文件拆分为多个变量 - Trying to split a txt file into multiple variables Python如何去读取.txt文件中的行并将其拆分为列表? - How does Python go about reading over lines in a .txt file and split them into a list? 将多个txt文件中的行读入变量 - Read lines from a multiple txt files into variables 如何将我的变量从程序保存到txt文件 - How to save my variables to a txt file from a program 在Python中,如何从空格分隔的.txt文件中获取整数列表,并在多行上使用'\\ r \\ n'分隔数字? - In Python, how to get integer lists from a .txt file with space separated and '\r\n' delimited numbers on multiple lines?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM