Edit2: I realized the title of the thread was misleading after the post was made..it's more like There's 6 lines per student..I need to make a list for each student. So it's 6X..where X is the number of students.

Hi all..

I have a text file that looks like so:

Joe
Body
10429114
IT
30
90
Sue
Noone
12345678
ENG
45
103

I need to read in the objects in groups of 6 and create list. The catch is that the text file could have any specified number number of lines, as long as it is in increments of 6. The order of the data will always be in the some order.

For example: The output of that specific file should be something along the lines of:

[Joe, Body, 10429114, IT, 30, 90]
[Sue, Noone, 12345678, ENG, 45, 103]

Edit: Not looking for a full code..just guidelines.
Edit2: I realized the title of the thread was misleading after the post was made..

Edited 5 Years Ago by bigredaltoid: n/a

Maybe simplest to beginner to use normal loop with modulo 6 based append of parts. Last one must be added after loop.

>>> data = '''Joe
Body
10429114
IT
30
90
Sue
Noone
12345678
ENG
45
103'''
>>>> students = []
>>> for count, item in enumerate(data.splitlines()):
	if not count % 6:
		if count: students.append(student)
		student = []
	student.append(item)
else:
	students.append(student)

	
>>> students
[['Joe', 'Body', '10429114', 'IT', '30', '90'], ['Sue', 'Noone', '12345678', 'ENG', '45', '103']]
>>>

Edited 5 Years Ago by pyTony: n/a

My code is thus:

myFile = open('students.txt','r')
students = []
for count, item in enumerate(myFile.splitlines()):
    if not count % 6:
            if count: students.append(student)
            student = []
    student.append(item)

else:
        students.append(student)
print students

When I try and run it it says that file object has no attribute 'splitlines'

No but it has something similar and then you ae also required to at least understand code and not only copy paste.

My mistake. I just woke up not long ago and am making stupid mistakes. I can get the items into separate list, but they print with a '\n' after each line. I know I can use a .strip method to remove the characters, but how would I incorporate that into the code?

Something like?

line = line.rstrip('\n')

Edited 5 Years Ago by bigredaltoid: n/a

This question has already been answered. Start a new discussion instead.