A small problem with the %ci format (solution included)
Closed this issue · 1 comments
Dear Phil Hagen,
Good morning. How are you?
The ip2geo has worked for almost all format options except the %ci:
[root@ol72 ip2geo]# echo 190.93.240.233 | ./ip2geo.py -f %ci
Traceback (most recent call last):
File "./ip2geo.py", line 160, in
output_string = output_re.sub(lambda x: str(ipdata[format_map[x.group()][0]]), args.format)
File "./ip2geo.py", line 160, in
output_string = output_re.sub(lambda x: str(ipdata[format_map[x.group()][0]]), args.format)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)
Unfortunately, in a very bad luck case, the city would be San José and the last letter can be a problem to convert to ASCII text. Therefore, I have modified your program to use Unicode and instead of reading:
output_string = output_re.sub(lambda x: str(ipdata[format_map[x.group()][0]]), args.format)
Now we read the following line:
output_string = output_re.sub(lambda x: (u''.join(ipdata[format_map[x.group()][0]]).encode('utf-8')), args.format)
My recommendation is to avoid using str(), if possible.
I am sorry for touching in your program. Anyway, it is a good work. Congratulations.
I hope you have a nice day and thank you for the attention.
Alexandre Borges.
Thank you for the great feedback and fix, Alexandre - I'll get this integrated as soon as I have a chance.