Create list in list from file
I am trying to create a Python list in a list by reading a file. The file contains 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 generated by the code is not correct 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 generated 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 wrong list is preventing me from getting 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 anyone tell me how to create the correct list?
+3
source to share
1 answer
To get the desired result, you need to do several things.
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 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])))
This creates a list generated from a file (formatting changed for 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]
]
+3
source to share