如何在python中从文本文件向下读取一行

时间:2018-11-16 21:31:58

标签: python file

我有一个带有以下行的txt文件:

ENBO => [
 'h4d gh34245 ran54'
]

我希望能够将包含h4d gh34245 ran54放在我的python脚本的变量中。

我的python脚本:

f = open(txt.txt, "r")

for line1 in f:
         if ("ENBO" in line1):
               print (line1)

但是,这只是打印ENBO => [,但是我想要一种方法来读取当前行ENBO => [下的行以获取行h4d gh34245 ran54并将其存储在脚本中的变量内因此我可以据此阅读。另外,我也不想更改txt文件。我想专门搜索ENBO的包含物,而不是硬编码搜索h4d gh34245 ran54

7 个答案:

答案 0 :(得分:1)

Use a context manager to loop over the file and print/store the next value if a line of interest is found:

with open('txt.txt', "r") as f:
    for line in f:
        if 'ENBO' in line:
            print(next(f)) #you can also append the values to a list here
        else:
            #do something here*
            pass

>>'h4d gh34245 ran54'

 'h4d gh34245 ran54'

 'h4d gh34245 ran54'

 'h4d gh34245 ran54'

You can do this because f is a generator, it prints the next line if ENBO and continues after the next line.

This is tested in a mock text file:

ENBO => [
 'h4d gh34245 ran54'
]

ENBO => [
 'h4d gh34245 ran54'
]

ENBO => [
 'h4d gh34245 ran54'
]

ENBO => [
 'h4d gh34245 ran54'
]

答案 1 :(得分:0)

类似的事情应该起作用。

print_next_line = False
for line1 in f:
    if print_next_line:
        print(line1)
        print_next_line = False
    if "ENBO" in line1:
        print_next_line = True

答案 2 :(得分:0)

The answer yper gave should work, but may I also suggest looking into JSON file formatting? This would allow you to assign a value to the key "ENBO" and then access that through a key:value pairing?

Not sure what you're reading the file for, or what generates it so can't guarantee that this approach would help you.

答案 3 :(得分:0)

Do you mean that you wish to put the contents of a text file into a variable? There are two ways you would do this, the first of which is just to put it all into one string (with only one line I think this is what you want):

f = open(txt.txt, "r") # opening the file

output = f.read().replace('\n', '') # replacing the newline with spaces

You can just remove a bit of the second line to put it into an array of the lines.

output = f.read()

答案 4 :(得分:0)

首先,重要的是要注意您的文件包含3行,即使从语义上说这3行仅表示一个实体。

现在,如果您的文件确实如此简单,并且您只需要第二行,则可以使用方法readlines()。 这将读取整个文件并返回一个列表,其中文件的每一行都由一项表示。

然后,如果您知道行始终位于第二行(索引1),则可以直接访问它。

这是建议的解决方案:

f = open(txt.txt, "r")
all_lines = f.readlines()
requested_line = all_lines[1]

此外,我建议您使用with语法打开文件,以便在不再使用资源时将其丢弃:

with open(txt.txt, "r") as f:
    all_lines = f.readlines()
    requested_line = all_lines[1]

您可以在docsdeveloper's guide

中进一步了解with语句

请注意,readlines()将遍历整个文件,因此,如果文件长度未知,则应避免使用它。

答案 5 :(得分:0)

我处理类似问题的通常方法是先读取,直到找到表明所要数据开始的行,然后收集所需的数据。对于此问题,应执行以下操作:

f = open('txt.txt', "r")
for line1 in f:
    if ("ENBO" in line1):
        break                 # Stops the loop
if f:                         # Make sure you didn't hit the end of the file
    data_line = f.readline()  # Grab the next line
    print(data_line)

答案 6 :(得分:0)

我建议您这样做:

from pathlib import Path

print(Path(MY_FILE).read_text().splitlines()[1])

强烈建议使用pathlib进行文件操作。如果您不能使用它,则等效:

with open(MY_FILE) as f:
    print(f.readlines()[1])