1

How do I separate the values using Python? I've tried split and linespace but they don't split the data in the way I'm expecting

My .txt file contains the following:

{0: 'tench, Tinca tinca', 
1: 'goldfish, Carassius auratus', 
2: 'great white shark, white shark, man-eater, man-eating carcharias', 3: 'tiger shark, Galeocerdo cuvieri', 
4: 'hammerhead, hammerhead shark', 
5: 'electric ray, crampfish, numbfish, torpedo',} 

I'm looking for an output key = [0,1,2,3,...] Values = ['tench, Tinca tinca','goldfish, Carassius auratus',...] or can I just somehow convert it into a dictionary? I've tried split using the argument (',') and it splits 'tench, but I want 'tench, Tinca tinca' as an output.

This is the code I'm stuck on

f = open('imagenet1000_clsid_to_human.txt', 'r') 
x = f.read().lower().strip().split("',") 
y = [] 
for i in x: (y.append(i)) 
    print(y)
NOOB
  • 2,717
  • 4
  • 22
  • 22
DJ29
  • 31
  • 3

4 Answers4

3

The key idea is to read the raw text as dict.

import ast
with open('imagenet1000_clsid_to_human.txt', 'r') as f:
    s = f.read()
    dicts = ast.literal_eval(s)
print(list(dicts.keys()))
print(list(dicts.values()))

Output

[0, 1, 2, 3, 4, 5]
['tench, Tinca tinca', 'goldfish, Carassius auratus', 'great white shark, white shark, man-eater, man-eating carcharias', 'tiger shark, Galeocerdo cuvieri', 'hammerhead, hammerhead shark', 'electric ray, crampfish, numbfish, torpedo']
ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45
1

Dirty hack:

a = re.findall("(\d+): \'(.*?)\'", txt)
keys, values = zip(*a)

Other dirty hack:

txt = txt.replace("'", '"').replace(",}", "}")  
txt = re.sub("(\d+):", r'"\1":', txt)
data = json.loads(txt)

Of cource, you should import re or json respectively.

Hyyudu
  • 134
  • 1
  • 7
1

If you want to change from str representation of the text file to dict, use:

str_to_dict = ast.literal_eval(x)

Once you have a dict, if I understand correctly, you want 1 list of all the keys and other list containing all the values. For that, you can do this:

keys = []
values = []
for key,val in str_to_dict.items():
   keys.append(key)
   values.append(val)
NOOB
  • 2,717
  • 4
  • 22
  • 22
1

Assumming you can store the contents of the file in a str:

text = "0: 'tench, Tinca tinca', 1: 'goldfish, Carassius auratus', 2: 'great white shark, white shark, man-eater, man-eating carcharias', 3: 'tiger shark, Galeocerdo cuvieri', 4: 'hammerhead, hammerhead shark', 5: 'electric ray, crampfish, numbfish, torpedo',"

text_list = list(filter(lambda x: len(x) > 0, text.split("',")))
keys = list(map(lambda x: x.split(":")[0], text_list))
values = list(map(lambda x: x.split(":")[1][2:], text_list))
my_dict = dict(zip(keys,values))

Not the most elegant solution, but works if you don't want to use other packages as ast. However, I recommend you using the ast packages as described in the answers provided by other users as it can deal with different formats/spacing so you don't have to worry about that.