Python Forum
Getting extra charter in result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting extra charter in result
#1
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
Reply
#2
Try k, v = line.strip().split()
Reply
#3
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An Extra 'None' leoahum 5 3,925 Oct-18-2018, 08:20 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

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