Python Forum
unable to convert text file in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to convert text file in dictionary
#1
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
Reply
#2
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
Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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']
Reply
#5
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')
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 1 2,084 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Python is unable to read file Genericgamemaker 13 3,553 Jul-19-2024, 06:42 PM
Last Post: snippsat
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 9,595 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Convert File to Data URL michaelnicol 3 2,594 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Python Script to convert Json to CSV file chvsnarayana 8 4,639 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,982 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Convert Excel file into csv with Pipe symbol.. mg24 4 2,239 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 3,935 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,091 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  convert a named tuple to a dictionary Skaperen 13 6,064 Mar-31-2022, 07:13 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020