Reputation: 73
so I have a little data and what I'm trying to do is create a list of each line and append it into another list, but I could not come up with any idea about how to print like only the first or second line. I tried to split the entire data and then make a list of each line using indexing methods, but that's how you do it manually, but I want to make it automatically. So, the help would be much appreciated.
students = "joe 10 15 20 30 40 \
bill 23 16 19 22 \
sue 8 22 17 14 32 17 24 21 2 9 11 17 \
grace 12 28 21 45 26 10 \
John 14 32 25 16 89"
file_to_save = open("students.txt", "w")
file_to_save.writelines(students)
file_to_save.close()
file_to_read = open("students.txt", "r")
line = file_to_read.readline()
print(line)
file_to_read.close()
the output could look like:
test = [[joe, 10, 15, 20, 30, 40], [bill, 23, 16, 19, 22], etc...]
Upvotes: 0
Views: 122
Reputation: 505
The problem is due to the write string. You are entering data in one single long line and able to read it in one call.
NOTE: \
is different \n
in string.
To solve this one, I agree with the solution of pre-processing data before adding it into file from @abhilb
Just using his code and wrapping my code to solve your problem.
import re
students = "joe 10 15 20 30 40 \
bill 23 16 19 22 \
sue 8 22 17 14 32 17 24 21 2 9 11 17 \
grace 12 28 21 45 26 10 \
John 14 32 25 16 89" ## Still a big chunk of line for your record.
lines = [x.strip() for x in re.findall(r'\w+[\s\d]+', students)] # Credit to @abhilb
with open("students.txt", "w") as file_to_save: # Use this method to lose the hastle to close a file..
for line in lines: # Lines Formed in the Students record as List of Strings...
print(line,file=file_to_save) # Saving them to the file with '\n' at the end..
index_of_lines_to_read = {1,3} # Index of the line you want to print.
with open("students.txt", "r") as file_to_read: # Use this method to lose the hastle to close a file..
line = file_to_read.readline() # Reading first line...
line_index = 1 # Keep check about the index we have read so far..
while line: # Checking if we did not reach the EOF
if line_index in index_of_lines_to_read: # Checking if the current index needs to be printed else skip to next line.
print(line,end="") # end="" compensate for the extra \n added using print above
line = file_to_read.readline() # Read next line..
line_index += 1 # Increment the current line index.
Output :
joe 10 15 20 30 40
sue 8 22 17 14 32 17 24 21 2 9 11 17
Upvotes: 1
Reputation: 5757
You could use regex to split the data into lists
>>> import re
>>> lines = [x.strip() for x in re.findall(r'\w+[\s\d]+', students)]
>>> lines
['joe 10 15 20 30 40', 'bill 23 16 19 22', 'sue 8 22 17 14 32 17 24 21 2 9 11 17', 'grace 12 28 21 45 26 10', 'John 14 32 25 16 89']
Upvotes: 1
Reputation: 4510
If what you want is to read a file and then print only a certain line you can try something like this:
def print_file_line(file_path: str, line_number: int):
with open(file_path) as file:
lines = file.readlines()
print(lines[line_number])
print_file_line('students.txt', 1)
>>> '[bill, 23, 16, 19, 22]'
print_file_line('students.txt', 0)
>>> '[joe, 10, 15, 20, 30, 40]'
Upvotes: 0
Reputation: 499
You can use python itertools groupby for this
from itertools import groupby
students = "joe 10 15 20 30 40 \
bill 23 16 19 22 \
sue 8 22 17 14 32 17 24 21 2 9 11 17 \
grace 12 28 21 45 26 10 \
John 14 32 25 16 89"
Data=students.split(' ')
[list(g) for k,g in groupby(Data,key=lambda x:x == '') if not k]
Output:
[['joe', '10', '15', '20', '30', '40'], ['bill', '23', '16', '19', '22'], ['sue', '8', '22', '17', '14', '32', '17', '24', '21', '2', '9', '11', '17'], ['grace', '12', '28', '21', '45', '26', '10'], ['John', '14', '32', '25', '16', '89']]
Upvotes: 1