0

I needed help to add into directory below text file. Can anyone can help me to do it? I tried
I have data.txt like below:?

A1234 161
A1234 106
A456  185
A456  108
037   125

**Output:**
directory = {
"A1234": [161,106],
"A456": [185,108],
"037": [125],
}

Thank you in advance for your help.

3 Answers 3

1

data.txt:

A1234 161
A1234 106
A456  185
A456  108
037   125

From file to dictionary:

with open('data.txt', 'r') as file:
    data_lines = file.readlines()

directory = {}

for line in data_lines:
    a, *b = line.split()
    # convert all elements of b into integers:
    b = [int(item) for item in b]
    if directory.get(a, False):
        if isinstance(b, list):
            directory[a].extend(b)
        else:
            directory[a].append(b)
    else:
        directory[a] = list(b)

print(directory)
# {'A1234': [161, 106], 'A456': [185, 108], '037': [125]}
# prettified:
"""
{
    'A1234': [161, 106],
    'A456': [185, 108],
    '037': [125]
}
"""
Sign up to request clarification or add additional context in comments.

12 Comments

For above file your code works fine. Thank you. But when I use other file which has 9375 rows of data, Its throw error like "ValueError: too many values to unpack (expected 2)".
I'm trying to do ICD mapping file dynamically added into directory.
Ok, I think ValueError: too many values to unpack (expected 2) has nothing to do with the amount of data. It has to do with the line a, b = line.split() : it's unpacking the line split (a list) in two variables. Does your data file contain lines with more than 2 'words' ? (I guess so. If so, show me an exemple I will adapt the code) See more info here
I tested the code with the example input data just by changing the first line to A1234 161 55 and that's throwing the same error. I edited my answer, now it should work fine with any amount of 'words' or 'numbers' per line :)
Awesome, Stijn B Sir, It works. Thank you.
|
0

Try this:

output_dict = {}
data = list(map(
    lambda a:a.strip().split(),
    open("data.csv").readlines()
))
for k,v in data:
    try:
        output_dict[k].append(v)
    except:
        output_dict[k] = [v]
output_dict

Output:

{'A1234': ['161', '106'], 'A456': ['185', '108'], '037': ['125']}

2 Comments

This works, but using a bare exception clause is generally considered to be a bad practice. See here for more. stackoverflow.com/questions/14797375/…
For above file your code works fine. Thank you. But when I use other file which has 9375 rows of data, Its throw error like "ValueError: too many values to unpack (expected 2)".
0

Here's a pandas solution. First we employ read_csv() and use one or more spaces as our delimiter. We need to specify string (object) dtypes to get string values for the list items as in your output. If you want ints, you could skip the dtype argument and pandas will infer them. Next we groupby the fist column (0) and convert the values in column 1 to a list with apply. Finally, we use .to_dict to get a dictionary.

import pandas as pd 

df = pd.read_csv('dir.txt', header=None, sep=r"[ ]{1,}", dtype='object')

directory = df.groupby(0)[1].apply(list).to_dict()

output:

{'037': ['125'], 'A1234': ['161', '106'], 'A456': ['185', '108']}

2 Comments

For above file your code works fine. Thank you. But when I use other file which has 9375 rows of data, Its throw error like "ValueError: too many values to unpack (expected 2)".
You likely have some irregularities in your .txt file, but it's tricky to account for them without seeing the full data. Can you post a larger sample?

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.