et_phonehome
et_phonehome

Reputation: 137

Create a list in a list from a file

I am trying to create a Python list in a list by reading a file. The file contain the lines:

12, 'MWSTRES1', 'K1317055', 1
15, 'MWSTRES1', 'K1416241', 3
13, 'MWSTRES1', 'K1316235', 8
9, 'BTBITSPQ', 'J03016235', 3
1, 'PLAUSP01', 'K1316235', 2
2, 'VTNTBMOT', 'K1316237', 9
4, 'VTNTBMOT', 'K1316239', 13

Code:

combs = []
with open('file3', 'r') as f:
    result = [line.split(',')[:4] for line in f]
print(result)
print(sorted(result, key=lambda t: (t[0], t[1])))

The generated list produced by the code is incorrect as shown:

[['12', " 'MWSTRES1'", " 'K1317055'", ' 1\n'], ['15', " 'MWSTRES1'", " 'K1416241'", ' 3\n'], ['13', " 'MWSTRES1'", " 'K1316235'", ' 8\n'], ['9', " 'BTBITSPQ'", " 'J03016235'", ' 3\n'], ['1', " 'PLAUS
P01'", " 'K1316235'", ' 2\n'], ['2', " 'VTNTBMOT'", " 'K1316237'", ' 9\n'], ['4', " 'VTNTBMOT'", " 'K1316239'", ' 13']]

I want the created list to look like this:

[[12, 'MWSTRES1', 'K1317055', 1], [15, 'MWSTRES1', 'K1416241', 3], [13, 'MWSTRES1', 'K1316235', 8], [9, 'BTBITSPQ', 'J03016235', 3], [1, 'PLAUSP01', 'K1316235', 2], [2, 'VTNTBMOT', 'K1316237', 9], [4, 'VTNTBMOT', 'K1316239', 13]]

The incorrect list is preventing me from obtaining the correct sorted output:

[['1', " 'PLAUSP01'", " 'K1316235'", ' 2\n'], ['12', " 'MWSTRES1'", " 'K1317055'", ' 1\n'], ['13', " 'MWSTRES1'", " 'K1316235'", ' 8\n'], ['15', " 'MWSTRES1'", " 'K1416241'", ' 3\n'], ['2', " 'VTNTBMOT'", " 'K1316237'", ' 9\n'], ['4', " 'VTNTBMOT'", " 'K1316239'", ' 13'], ['9', " 'BTBITSPQ'", " 'J03016235'", ' 3\n']]

Can someone tell me how to create the correct list?

Upvotes: 3

Views: 57

Answers (1)

hgazibara
hgazibara

Reputation: 1832

There are a few things you need to do to obtain the desired result.

def clean_line(line):
    # for the sake of completeness, remove surrounding blanks from all columns
    parts = [part.strip() for part in line.strip().split(',')[:4]]

    # first turn first and last columns to integers
    parts[0] = int(parts[0])
    parts[-1] = int(parts[-1])

    # single quotes need to be removed from the columns in the middle
    parts[1:-1] = [part.strip("'") for part in parts[1:-1]]
    return parts

In the end, your code should probably look something like this:

combs = []
with open('file3', 'r') as f:
    result = [clean_line(line) for line in f]
print(result)
print(sorted(result, key=lambda t: (t[0], t[1])))

Using this, list created from the file looks like this (formatting changed for the sake of readability):

[
    [12, 'MWSTRES1', 'K1317055', 1],
    [15, 'MWSTRES1', 'K1416241', 3],
    [13, 'MWSTRES1', 'K1316235', 8],
    [9, 'BTBITSPQ', 'J03016235', 3],
    [1, 'PLAUSP01', 'K1316235', 2],
    [2, 'VTNTBMOT', 'K1316237', 9],
    [4, 'VTNTBMOT', 'K1316239', 13]
]

Upvotes: 3

Related Questions