1

In Python 3 I've created an empty dictionary with the following keys:

default_value = ''

sq = dict.fromkeys(['Num','ID','Cognome','Data','altezza','V6','spec_role','V8','Nome','Ruolo','nick','trasf','straniero','pict','V15','V16','V17'],default_value)

I don't know how to load values from a string like this sequentially:

line = '1   LUC-TOR Tortuga 10/13/1991              LUC-TOR Luca    2   Tortuga         33524.7 '

to obtain:

mydict = { 
    "Num": 1,
    "ID": LUC-TOR,
    "Cognome": Tortuga,
    "Data": 10/13/1991
    "altezza":
    "V6": 
    "spec_role":
    "V8":
    "Nome": Luca
    "Ruolo": 2
    "nick": Tortuga
    "trasf":
    "straniero":
    "pict":
    "V15":
    "V16":
    "V17": 33524.7
}

What is the best pythonic way to do it?

1
  • I've forgot to say that the string is tab separated. For that I can use the string.split method as I know. Commented Sep 9, 2021 at 15:41

4 Answers 4

3

Use split() to split the string into a list of values. Then you can combine it with the list of keys.

keys = ['Num','ID','Cognome','Data','altezza','V6','spec_role','V8','Nome','Ruolo','nick','trasf','straniero','pict','V15','V16','V17']
my_dict = dict(zip(keys, line.split('\t')))
Sign up to request clarification or add additional context in comments.

Comments

2

You could use zip:

keys = ['Num','ID','Cognome','Data','altezza','V6','spec_role','V8','Nome','Ruolo','nick','trasf','straniero','pict','V15','V16','V17']

values = line.split('\t')

mydict = dict(zip(keys, values))

zip produces a sequence of tuple pairs, e.g. ('Num', '1'), ('ID', 'LUC-TOR'), ...

If you wanted you could also have a list of types to convert the values to:

types = [int, str, str, date, ...]

mydict = {key: _type(value)
          for key, value, _type in zip(keys, values, types)}

1 Comment

Thanks a lot I didn't know the zip function.
0

use zip() and split():

keys = ['Num','ID','Cognome','Data','altezza','V6','spec_role','V8','Nome','Ruolo','nick','trasf','straniero','pict','V15','V16','V17']
mydict = dict(zip(keys, line.split('\t')))

Comments

0

If you need to convert your line to array, there is my func:

def convert_to_array(value):
    result_line = ''
    white_space_prev=False
    for el in line:
        if el!=' ':
            if white_space_prev:
                result_line+=' '+el
            else:
                result_line+=el
            white_space_prev=False
        else:
            white_space_prev=True
    return result_line.split(' ')
line = '1    LUC-TOR Tortuga 10/13/1991              LUC-TOR Luca    2   Tortuga         33524.7 '
print(convert_to_array(line))

result:

['1', 'LUC-TOR', 'Tortuga', '10/13/1991', 'LUC-TOR', 'Luca', '2', 'Tortuga', '33524.7']

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.