'''def get_us_value(fp):
'''Insert docstring here.'''
print()
print(fp.readline())
for x in fp:
for y in x.split():
if y == 'States':'''
I would like to print the next line after states in order to get the number associated with United States but I am currently stuck on how to print the next line after states. This is the.txt file
United
States
91.9
You can use \n.
For example: if you do print(“foo \nbar”) foo is line one, bar is line two.
Look at this, it can be helpful: https://www.google.co.uk/amp/s/cmdl.netips.com/2012/09/three-ways-to-write-text-to-a-file-in-python/amp/
You can use the next
function on the file object to obtain the next line:
for x in fp:
if 'States' in x.split():
print(next(fp), end='')
You may not pick this solution as I don't know whether you can use regex or not but you can use regex to get the matching result. I don't know what the use case actually is but assuming you only have those lines in a text file, the regex States\n(.*)
should match the whole line after the keyword States
.
import re
print(re.findall(r"States\n(.*)", fp.read())[0])
Output:
91.9
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.