Chris
Chris

Reputation: 1

Importing nested list into a text file

I've been working on a problem which I realise I am probably approaching the wrong way but am now confused and out of ideas. Any research that I have done has left me more confused, and thus I have come for help.

I have a nested list:

[['# Name Surname', 'Age', 'Class', 'Score', '\n'], ['name', '9', 'B', 'N/A', '\n'], ['name1', '9', 'B', 'N/A', '\n'], ['name2', '8', 'B', 'N/A', '\n'], ['name3', '9', 'B', 'N/A', '\n'], ['name4', '8', 'B', 'N/A', '']]

I am trying to make it so this list is imported into a text file in the correct layout. For this I flattened the string and then joined it together with ','.

The problem with this is that because the '\n' is being stored in the list itself, it adds a comma after this, which ends up turning this:

Name Surname,Age,Class,Score,

Name,9,B,N/A,

Name1,9,B,N/A,

Name2,8,B,N/A,

Name3,9,B,N/A,

Name4,8,B,N/A,

into:

Name Surname,Age,Class,Score,

,

,Name,9,B,N/A,

,Name1,9,B,N/A,

,Name2,8,B,N/A,

,Name3,9,B,N/A,

,Name4,8,B,N/A,

If I remove the \n from the code the formatting in the text file is all wrong due to no new lines.

Is there a better way to approach this or is there a quick fix to all my problems that I cannot see?

Thanks!

My code for reference:

def scorestore(score):

    user[accountLocation][3] = score


    file = ("classdata",schclass,".txt")
    file = "".join(file)

    flattened = [val for sublist in user for val in sublist]

    flatstring = ','.join(str(v) for v in flattened)



    accountlist = open(file,"w")

    accountlist.write(flatstring)

    accountlist.close()

Upvotes: 0

Views: 279

Answers (4)

Danielle M.
Danielle M.

Reputation: 3662

I'm not sure which list is the one in your post (sublist?) but when you flatten it, just discard the "\n" strings:

flattened = [x for x in sublist if x != ["\n"]]

Upvotes: 1

Hai Vu
Hai Vu

Reputation: 40688

Use the csv module to make it easier:

import csv

data = [
    ['# Name Surname', 'Age', 'Class', 'Score','\n'],
    ['\n'],
    ['Name', '9', 'B', 'N/A','\n'],
    ['Name1', '9', 'B', 'N/A','\n'],
    ['Name2', '8', 'B', 'N/A','\n'],
    ['Name3', '9', 'B', 'N/A','\n'],
    ['Name4', '8', 'B', 0]
]

# Remove all the ending new lines
data = [row[:-1] if row[-1] == '\n' else row for row in data]

# Write to file
with open('write_sublists.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(data)

Discussion

Your data is irregular: some row contains the ending new line, some row don't. Yet some row contains all strings and some row contains a mixed data type. The first step is to normalize them by remove all ending new lines. The csv module can take care of mixed data types just fine.

Upvotes: 0

Tammo Heeren
Tammo Heeren

Reputation: 2104

Instead of making one string, how about writing lines. Use something like this:

 list_of_list = [[...]]
 lines = [','.join(line).strip() for line in list_of_list]
 lines = [line for line in lines if line]
 open(file,'w').writelines(lines)

Upvotes: 0

user3030010
user3030010

Reputation: 1857

The easiest way would probably be to remove the newlines from the sublists as you get them, the print each sublist one at a time. This would look something like:

for sublist in users:
    print(",".join(val for val in sublist if not val.isspace()), file=accountlist)

This will fail on the 0 in your list, however. I'm not sure if you intend to handle that, or if it's extraneous. If you do need to handle is, then you'll have to change the generator expression to str(val) for val in sublist if not str(val).isspace().

Upvotes: 0

Related Questions