r/Python • • Feb 23 '20

Help Question about text files

[removed] — view removed post

0 Upvotes

5 comments sorted by

View all comments

1

u/WinterMelonToufu Feb 23 '20

Iterate with a counter to find the lines, split string/regex to extract the data you need from the specific line.

1

u/[deleted] Feb 23 '20

I don't know how to split the notepad file into separate variables for each line

0

u/WinterMelonToufu Feb 23 '20

line_2_data = None
line_3_data = None
line_4_data = None

with open('some_file.txt') as file:
 # iterate line by line
 for line in file:
   if this_line_is_what_i_want(line):
      line_2_data = extract_data_using_regex_or_split_string(line)
   elif this_line_is_what_i_want_too(line):
      line_3_data = extract_this_data_too(line)
   ...

if index of the line in the file is what you are looking for.

with .... as file:
  lines = [line for line in file]
  line_2_data = extract_data_using_regex_or_split_string(lines[1])
  line_3_data = extract_this_data_too(lines[2])