Hi Team,
I am trying to convert one text file in dictionary but it remains empty . Please help .
text files sample records are as follows
CC_FIPS FULL_NAME_ND
AN Aixas
AN Aixirivall
AN Aixovall
AN Andorra la Vella
AN Ansalonga
AN Anyos
AN Arans
AN Arinsal
AN Aubinya
AN Bixessarri
There is tabsapce between column as per my understading
import json
filename='C:/Users/pubhatia/Documents/learning/python/query/GEODATASOURCE-CITIES.TXT'
#creating states dictionary object
states ={}
with open (filename) as fh:
for line in fh:
line=line.split(' ')
if not line: # empty line?
print(line)
continue
states[line[0]] = line[1:]
if not bool(states):
print ('Dict is Empty')
Output:
['AF', 'Biland\n']
['\n']
['AF', 'Biland Hawa\n']
Dict is Empty
Indentation issue.. move line 15 back 4 spaces. As it is now, it's part of
the if statement on line 12, which you don't want
Note that after fixing this issue your code will not produce meaningful result as the key for dict is repetitive and previous value will be overwritten when you assign next value for same state.
As mention key will be overwritten because of key collision.
Can use defaultdict which can collect values in a list.
Output:
CC_FIPS FULL_NAME_ND
AN Aixas
AN Aixirivall
AN Andorra la Vella
AF Biland
AF Biland Hawa
from collections import defaultdict
d = defaultdict(list)
with open('to_dict.txt') as f:
next(f)
for line in f:
line = line.strip()
line = line.split(' '*3)
if not line == ['']:
d[line[0]].append(line[1].strip())
Test:
>>> d
defaultdict(<class 'list'>,
{'AF': ['Biland', 'Biland Hawa'],
'AN': ['Aixas', 'Aixirivall', 'Andorra la Vella']})
>>> d['AF']
['Biland', 'Biland Hawa']
hi All,
I have changed my code and its working fine. But yes Thanks for giving new solution of defult dict.
I have shared sample data , I have 2.5 million of rows and collision can happen so should I
this method
import json
filename='C:/Users/pubhatia/Documents/learning/python/query/GEODATASOURCE-CITIES_1.TXT'
#creating states dictionary object
states ={}
with open (filename) as fh:
for line in fh:
print(line)
if line.strip():
k,v=line.split(' ',1)
states[k] = v.split()
if not bool(states):
print ('Dict is Empty')
Btw, checking for an empty variable is simple enough.
>>> states = {}
>>> if states:
... print('Non empty!!!')
...
>>> # See? Nothing :-)
None, False, 0, 0.0, an empty string, variable, tuple, list, etc. ( all empty sequences ) evaluates to False.
(Mar-29-2018, 12:39 PM)snippsat Wrote: [ -> ]As mention key will be overwritten because of key collision.
Can use defaultdict which can collect values in a list.
Output:
CC_FIPS FULL_NAME_ND
AN Aixas
AN Aixirivall
AN Andorra la Vella
AF Biland
AF Biland Hawa
from collections import defaultdict
d = defaultdict(list)
with open('to_dict.txt') as f:
next(f)
for line in f:
line = line.strip()
line = line.split(' '*3)
if not line == ['']:
d[line[0]].append(line[1].strip())
Test:
>>> d
defaultdict(<class 'list'>,
{'AF': ['Biland', 'Biland Hawa'],
'AN': ['Aixas', 'Aixirivall', 'Andorra la Vella']})
>>> d['AF']
['Biland', 'Biland Hawa']
I was running this code but getting error :
IndexError: list index out of range