Reputation: 95
I have a txt file with the following line:
ENBO => [
'h4d gh34245 ran54'
]
I want to be able to put the containments h4d gh34245 ran54
inside a variable in my python script.
My python script:
f = open(txt.txt, "r")
for line1 in f:
if ("ENBO" in line1):
print (line1)
However this just prints ENBO => [
, but I want a way to read the line below my current line ENBO => [
to get the line h4d gh34245 ran54
and store it inside of a variable in my script so I may read from it accordingly. Also, I do not want to change the txt file in anyway. And I want to search for the containments of ENBO
specifically, not hard-code search for h4d gh34245 ran54
Upvotes: 2
Views: 3584
Reputation: 1740
First of all, it is important to note that your file contains 3 lines, even if semantically those 3 lines represent only one entity.
Now, if your file is really this simple, and you really just want the second line, you can use the method readlines(). This will read the whole file and return a list, where each line of the file is represented by one item.
Then, if you know that your line is always on the second line (index 1), you can just access it directly.
here is the suggested solution:
f = open(txt.txt, "r")
all_lines = f.readlines()
requested_line = all_lines[1]
Also, I would like to suggest that you use the with
syntax to open the file, so the resource is disposed of when it is no longer used:
with open(txt.txt, "r") as f:
all_lines = f.readlines()
requested_line = all_lines[1]
You can understand more about the with
statement in the docs or in the developer's guide
Note that readlines()
goes through the whole file, so if your file might be of an unknown length, you should probably refrain from using it.
Upvotes: 0
Reputation: 789
I would recommend just doing this:
from pathlib import Path
print(Path(MY_FILE).read_text().splitlines()[1])
Using pathlib
for your file operations is highly recommended. If you can't/won't use it, this is equivalent:
with open(MY_FILE) as f:
print(f.readlines()[1])
Upvotes: 0
Reputation: 18570
The usual way I approach something like this is to read until I find the line that signals the start of the data I'm looking for, then gather the desired data. For this question, something like the following should work:
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)
Upvotes: 0
Reputation: 5434
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'
]
Upvotes: 1
Reputation: 846
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()
Upvotes: 0
Reputation: 357
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.
Upvotes: 0
Reputation: 4895
Something like this should work.
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
Upvotes: 0