0

I have a text file which contains two lines of text. Each line contains student names separated by a comma.

I'm trying to write a program which will read each line and convert it to a list. My solution seems to make two lists but I don't know how to differentiate between the two as both lists are called "filelist". For example I may need to append to the second list. How would I differentiate between the two?

Or is it possible for it to create completely separate lists with different names? I want the program to be able to handle many lines in the text file ideally.

My code is:

    filelist=[]

    with open("students.txt") as students:

            for line in students:

                    filelist.append(line.strip().split(","))


    print(filelist)
6
  • 1
    Well if you want it to handle an arbitrary number of lines, then you won't be able to create a different list each time. In that case you will most likely need a nested list. Commented Oct 3, 2016 at 11:46
  • 1
    Could you show what is in studentds.txt exactly? Commented Oct 3, 2016 at 11:47
  • Rebecca, Sarah, Matthew, Charlie Jenny, Matthew, Laura, Nicky Commented Oct 3, 2016 at 11:47
  • Sorry, there should be 4 students on the first line and 4 on the second! Commented Oct 3, 2016 at 11:49
  • And you want each line in its own separate list? Commented Oct 3, 2016 at 11:49

4 Answers 4

2

You will have to create a multi-dimensional array like this:

text = open("file.txt")

lines = text.split("\n")
entries = []
for line in lines:
    entries.append(line.split(","))

If your file is

John,Doe
John,Smith

then entries will be:

[["John", "Doe"], ["John", "Smith"]]
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, how then would I differentiate between the lists should I wish to append to either one?
Isn't this answer doing exactly what the code in the question already does?
@Computing102 simply use list indexes. If you want the list of names for say, line 5. Just say for name in entires[5]:.
@Computing102: entries is only one list, that happens to be made of two other lists. You can access its elements like with any other list: entries[0] will return ["John", "Doe"] and entries[1] will return ["John", "Smith"].
@Computing102 if this answer solved, or helped solved your problem, then consider accepting it.
0

in your code, filelist will be considered as a list of list because you append a list to it, not an element, consider doing filelist += line.strip().split(",") which will concatenate the lists

2 Comments

Why not just use filelist.extend()?
@Mr.goosberry pretty much the same as mentioned there: link
0

two lines to done

with open("file.txt") as f:
    qlist = map(lambda x:x.strip().split(","), f.readlines())

or

with open("file.txt") as f:
    qlist = [i.strip().split(",") for i in f.readlines()]

Comments

0

If you are looking to have a list for each line, you can use a dictionary where the key is the line number and the value of the dictionary entry is the list of names for that line. Something like this

with open('my_file.txt', 'r') as fin:
    students = {k:line[:-1].split(',') for k,line in enumerate(fin)}

print students

The line[:-1] is to get rid off the carriage return at the end of each line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.