Python Forum

Full Version: Getting extra charter in result
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

When I am trying to create dictionary from file. I am getting some additional characters. Can anyone confirm why Am I getting this

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('	') 
			#print (k)
			#print (v)
			states[v] = k.split()
			
if  states:
	print('start for loop')
	for i in states:
		print (i,states[i])
Output:
start for loop Aixas ['AN'] Aixirivall ['AN'] Aixovall ['AN'] Andorra la Vella ['AN'] Ansalonga ['AN']
AS u can see some charters in first output
Data Stored in file is :

AN Aixas

AN Aixirivall

AN Aixovall

AN Andorra la Vella

AN Ansalonga
Try k, v = line.strip().split()
Hi All,
I got solution for this. We are getting  extra character because file is probably encoded in UTF-8 with BOM. So to resolve this we need to open file in below mentioned way :

data = open('info.txt', encoding='utf-8-sig')