简体   繁体   English

是否可以将 .txt 的每一行存储为一个列表,然后再使用它?

[英]Is it possible to store each line of a .txt into as a list and then use it later?

Im new to Python and trying to do some code on my own.我是 Python 新手,正在尝试自己编写一些代码。 I'd like to take input from a .txt file and then store each line of that .txt into a different list.我想从 .txt 文件中获取输入,然后将该 .txt 的每一行存储到不同的列表中。

For example, if the file has 2 lines, I want to do something like this:例如,如果文件有 2 行,我想做这样的事情:

.txt file: .txt 文件:

blue, red, yellow, orange
city, car, airplane, dinosaur

python output:蟒蛇输出:

list1 = [blue, red, yellow, orange]
list2 = [city, car, airplane, dinosaur]

I tried working with loops for extracting the content in form of lists, but I dont know how to store the actual results from the loop into different lists.我尝试使用循环来以列表的形式提取内容,但我不知道如何将循环中的实际结果存储到不同的列表中。 I'm currently using the open() function to work with the files, but can't figure how to achieve this.我目前正在使用 open() 函数来处理文件,但不知道如何实现。

Thanks!谢谢!

EDIT1: 23 Jun 2021编辑 1:2021 年 6 月 23 日

This is the work I'm using.这是我正在使用的工作。

with open('file.txt') as fh:
    for line in fh:
        print(line.split("\n"))

This code prints in the console 2 lists (because the file has 2 lines).此代码打印在控制台 2 列表中(因为该文件有 2 行)。 But I dont know how to store those lists in 2 differentes variables.但我不知道如何将这些列表存储在 2 个不同的变量中。

First you need to open the file as read mode.首先,您需要以读取模式打开文件。 You can handle it.你可以解决的。 A little help for you.对你有一点帮助。

Open File打开文件

If we imagine you have a lot of lines to take.如果我们想象你有很多台词要走。 You can use a loop to get all the lines step by step(You can get all lines as an array and then get them in loop or take one line in every step - I'm letting you to find this functions) and in every step you can use another loop inside of it and take the strings.您可以使用循环逐步获取所有行(您可以将所有行作为数组获取,然后将它们放入循环中或在每一步中获取一行 - 我让您找到这些函数)并且在每一步中您可以在其中使用另一个循环并获取字符串。 To do that you should use split for every line and get array of strings belongs that line.为此,您应该对每一行使用 split 并获取属于该行的字符串数组。 Split functions takes a string as a parameter and returns you an array that includes all that strings.拆分函数将字符串作为参数并返回一个包含所有字符串的数组。 You can check it.你可以检查一下。

Split Function拆分功能

But in your situation you want to create new array for every line in your example for 2 lines, you can take first line from file and split it and after that you can do this for other too.但是在您的情况下,您想为示例中的每一行为 2 行创建新数组,您可以从文件中取出第一行并将其拆分,然后您也可以为其他行执行此操作。 So you will get to 2 array includes lines.所以你会得到 2 个数组包含行。

But if you wonder how can i do this for uncertain number of line, you can use the first method that I mentioned and add all line arrays to an array at the end of every step in your first(inclusive) loop .但是,如果您想知道如何针对不确定的行数执行此操作,您可以使用我提到的第一种方法,并在第一个(包含)循环的每一步结束时将所有行数组添加到一个数组中。 So you will have all arrays that includes all line's string in an array.因此,您将拥有包含数组中所有行字符串的所有数组。 You can think like your first index of array will be your first line strings.您可以认为数组的第一个索引将是您的第一行字符串。

To imagine the figure you will get想象一下你会得到的数字

all_lines=[
    [blue, red, yellow, orange],
    [city, car, airplane, dinosaur],
    [one,two,three,four]
]

or something like that.或类似的东西。

After reading a bit more, found the answer by trial and error.多读一点后,通过反复试验找到了答案。 This is the code that serves my purpose这是符合我目的的代码

f = open("file.txt")
linea1 = f.readline().split(",")
linea2 = f.readline().split(",")

print(linea1)
print(linea2)

Output输出

linea1 = [city, building, ...]
linea2 = [hi, hello, blue ...]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM