简体   繁体   中英

Reading data from file with different number of components in each line in Python

In this project I need to read the input file with unknown data and put it in a dictionary. The first column will always be name, while the rest will always be values. However, we don't know have many column for the values can it contain. For example:

name1;1;2;3;4
name2;1;1;1
name3;5;6
name4;9

I was thinking using split(';') but each row might have different columns.

for line in file:
   line = line.rstrip()
   name, value = line.split(';')
ValueError: too many values to unpack

Wanted final result:

d={name1:[1,2,3,4], name2:[1,1,1], name3:[5,6], name4:[9]}

Thank you so much for your help!

You're almost there. Splitting your line and using multiple assignment is the right idea. The missing piece is that you can use * in multiple assignment to make one portion have a variable length. Check out this article for an excellent rundown of various uses and idioms.

result = {}

with open('name_of_your_file') as file:
    for line in file:
        # Set name to the first element, and set value to the rest.
        name, *value = line.rstrip().split(';')
        result[name] = value

Using split(';') is a good idea here, this should work for you:

filename = 'path_to_your_file'
res={}
with open(filename, 'r') as file: #opens your file in reading mode
    for line in file: #reads the file line by line
        temp = line.rstrip('\n') #removes ending newline characters
        temp = temp.split(';') #split string by ';' into a list
        res[temp.pop(0)]=temp #uses the first element of the list as dictionary key, and the rest of the list as value
print(res)

After reading lines into the list you can use dict comprehension to do this -

reading all lines into list -

list1 = ['name1;1;2;3;4',
'name2;1;1;1',
'name3;5;6',
'name4;9']

using dict comprehension -

result_dict  ={item.split(';')[0]: item.split(';')[1:] for item in list1}
print(result_dict) 

output -

{'name1': ['1', '2', '3', '4'],
 'name2': ['1', '1', '1'],
 'name3': ['5', '6'],
 'name4': ['9']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM