简体   繁体   中英

How to unpack values from a file

If as a input i have a file that read-

0->54:15
1->41:12
2->35:6
3->42:10
4->34:7
5->58:5
6->55:12
7->39:6
8->36:12
9->38:15
10->53:13
11->56:12
12->51:5
13->48:8
14->60:14
15->46:12
16->57:6
17->52:9
18->40:11

Actually this is an adjacency list. I want my code to read the file and take the values as -> u=0,v=54, w=15 and then go with my plan. How can i do this? Thank you in advance for your time to read and answer this.

Using.split would be good. For each line in the file (You can get this by using the open() function) split it using the arrow and the colon.

for line in lines:
    split_line = line.split("->") # Split by the arrow first
    split_line = split_line[0] + split_line[1].split(":")
    u, v, w = split_line # Note u, v, and w are strings

I would recommend using JSON format so you can use the json module in python the parse the file into variables easily.

If you had a single string:

import re

s = \
'''0->54:15
1->41:12
2->35:6
3->42:10
4->34:7
5->58:5
6->55:12
7->39:6
8->36:12
9->38:15
10->53:13
11->56:12
12->51:5
13->48:8
14->60:14
15->46:12
16->57:6
17->52:9
18->40:11'''

s = s.split('\n')
output = [re.split('->|:', x) for x in s]

output

[['0', '54', '15'], ['1', '41', '12'], ['2', '35', '6'], ['3', '42', '10'], ['4', '34', '7'], ['5', '58', '5'], ['6', '55', '12'], ['7', '39', '6'], ['8', '36', '12'], ['9', '38', '15'], ['10', '53', '13'], ['11', '56', '12'], ['12', '51', '5'], ['13', '48', '8'], ['14', '60', '14'], ['15', '46', '12'], ['16', '57', '6'], ['17', '52', '9'], ['18', '40', '11']]

If you want a dictionary

d = {x[0]:[x[1],x[2]] for x in output}

d

{'0': ['54', '15'], '1': ['41', '12'], '2': ['35', '6'], '3': ['42', '10'], '4': ['34', '7'], '5': ['58', '5'], '6': ['55', '12'], '7': ['39', '6'], '8': ['36', '12'], '9': ['38', '15'], '10': ['53', '13'], '11': ['56', '12'], '12': ['51', '5'], '13': ['48', '8'], '14': ['60', '14'], '15': ['46', '12'], '16': ['57', '6'], '17': ['52', '9'], '18': ['40', '11']}

If you want a dataframe:

import pandas as pd
df = pd.DataFrame(output, columns=['u','v','w'])

df

     u   v   w
0    0  54  15
1    1  41  12
2    2  35   6
3    3  42  10
4    4  34   7
5    5  58   5
6    6  55  12
7    7  39   6
8    8  36  12
9    9  38  15
10  10  53  13
11  11  56  12
12  12  51   5
13  13  48   8
14  14  60  14
15  15  46  12
16  16  57   6
17  17  52   9
18  18  40  11

Here is how you can use re.split() to split strings with multiple delimiters:

from re import split

with open('file.txt','r') as f:
    l = f.read().splitlines()

lst = [list(filter(None, split('[(\-\>):]',s))) for s in l]

print(lst)

Output:

[['0', '54', '15'],
 ['1', '41', '12'],
 ['2', '35', '6'],
 ['3', '42', '10'],
 ['4', '34', '7'],
 ['5', '58', '5'],
 ['6', '55', '12'],
 ['7', '39', '6'],
 ['8', '36', '12'],
 ['9', '38', '15'],
 ['10', '53', '13'],
 ['11', '56', '12'],
 ['12', '51', '5'],
 ['13', '48', '8'],
 ['14', '60', '14'],
 ['15', '46', '12'],
 ['16', '57', '6'],
 ['17', '52', '9'],
 ['18', '40', '11']]


Breaking it down:

This: lst = [list(filter(None, split('[(\-\>):]',s))) for s in l] is the equivalent of:

lst = [] # The main list
for s in l: # For every line in the list of lines
    uvw = split('[(\-\>):]',s) # uvw = a list of the numbers
    uvw = list(filter(None,uvw)) # There is an empty string in the list, so filter it out
    lst.append(uvw) # Add the list to the main list

I'm going to challenge the way that you're getting the input file in the first place: if you have any control over how you get this input, I'd encourage you to change its format. (If not, maybe this answer will help people who have a similar issue in the future).

There is typically little reason to "roll your own" serialization and deserialization like this - it's reinventing the wheel, given that most modern languages have built-in libraries to do this already. Rather, if at all possible, you should use a standard serialization and deserialization mechanism like Python pickle or a JSON serializer (or even a CSV, so that you can use a CSV parser).

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