Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with file handling
#1
for an assignment I have to Write a program that tells the user what the coordinates are for a given city. The issue is that Python will not read this text file. I've tried other text files and they work fine.

This is a portion of the text file:
Latitude Longitude City Province/State Country
82∞30N 62∞20W Alert Nunavut Canada
81∞36N 16∞40W Nord Greenland Denmark
79∞59N 85∞56W Eureka Nunavut Canada
78∞55N 11∞56E Ny-≈lesund Svalbard Norway
78∞13N 15∞39E Longyearbyen Svalbard Norway
77∞28N 69∞14W Qaanaaq Greenland Denmark
76∞25N 82∞54W Grise Fiord Nunavut Canada
73∞30N 80∞31E Dikson Krasnoyarsk Krai Russia
72∞47N 56∞09W Upernavik Greenland Denmark
71∞39N 128∞52E Tiksi Sakha Republic Russia
71∞32N 52∞19E Belushya Guba Arkhangelsk Oblast Russia
71∞18N 156∞46W Barrow Alaska United States
70∞59N 25∞59E HonningsvÂg Finnmark Norway
70∞40N 23∞41E Hammerfest Finnmark Norway
70∞12N 148∞31W Deadhorse Alaska United States
70∞05N 27∞53E Nuorgam Lapland Finland
70∞05N 29∞44E Vads¯ Finnmark Norway
69∞58N 23∞16E Alta Finnmark Norway
69∞54N 27∞01E Utsjoki Lapland Finland
69∞43N 30∞02E Kirkenes Finnmark Norway
69∞41N 18∞57E Troms¯ Troms Norway
69∞27N 133∞02W Tuktoyaktuk Northwest Territories Canada
69∞20N 88∞13E Norilsk Krasnoyarsk Krai Russia
68∞58N 33∞05E Murmansk Murmansk Oblast Russia
68∞47N 16∞32E Harstad Troms Norway
68∞25N 17∞34E Narvik Nordland Norway
68∞21N 133∞43W Inuvik Northwest Territories Canada
67∞51N 20∞13E Kiruna Norrbotten Sweden
67∞33N 133∞23E Verkhoyansk Sakha Republic Russia


The code I'm currently trying:
f = open('cities.txt','r')
for line in f:
print(line[:1])
f.close()


Everything I have tried returns something like this:
Traceback (most recent call last):
File "/Users/edgar/Desktop/Homework/ME 21 Codes/Week5Lab.py", line 2, in <module>
for line in f:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 50: invalid start byte


What is going on here? Working code would be appreciated.
Reply
#2
0xb0 is the degree symbol. The interpreter is saying that it cannot decode that byte using the UTF-8 encoding. In your call to open(), try this:

f = open("cities.txt", "r", encoding = "utf-16")
Reply
#3
use code tags. see BBCODE

Quote:The issue is that Python will not read this text file.

That cannot be. I have yet to find a file that cannot be read.

can you attach the actual file?
posting file may very well change the contents.

Is there a codec involved?
Reply
#4
(Sep-27-2018, 01:31 AM)Larz60+ Wrote: in

I can't figure out how to attach files so heres the contents. You can make a text file out of it if you'd like

Latitude Longitude City Province/State Country

(Sep-27-2018, 01:27 AM)stullis Wrote: 0xb0 is the degree symbol. The interpreter is saying that it cannot decode that byte using the UTF-8 encoding. In your call to open(), try this:

f = open("cities.txt", "r", encoding = "utf-16")

This was returned

Error:
Traceback (most recent call last): File "/Users/edgar/Desktop/Homework/ME 21 Codes/Week5Lab.py", line 3, in <module> for line in f: File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/encodings/utf_16.py", line 61, in _buffer_decode codecs.utf_16_ex_decode(input, errors, 0, final) UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 3692-3693: illegal UTF-16 surrogate
Reply
#5
please don't post files. You cannot post hidden characters, the only to do that is to attach the file
Click on New Reply, (don't use quick reply when attachments are required), Scroll down and you will find the 'Browse' Button.
find your attachments, then click add attachments. Not done yet, you now have to choose how to attach (inline and a couple of other options)
the error traceback is also showing you why you can't read the file:
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 3692-3693: illegal UTF-16 surrogate
meand you have to import codecs, and add a decode statement to your file, see: https://docs.python.org/3/library/codecs.html
Reply
#6
The problem is text that you post Unicode is already mess up.
Have to 5 post before can upload,can use this justbeamit and post link.

It read fine like this,you can try with utf-8 on original file.
with open('foo.txt', encoding='utf-8') as f:
    print(f.read()[:500])
Output:
82∞30N 62∞20W Alert Nunavut Canada 81∞36N 16∞40W Nord Greenland Denmark 79∞59N 85∞56W Eureka Nunavut Canada 78∞55N 11∞56E Ny-≈lesund Svalbard Norway 78∞13N 15∞39E Longyearbyen Svalbard Norway 77∞28N 69∞14W Qaanaaq Greenland Denmark 76∞25N 82∞54W Grise Fiord Nunavut Canada 73∞30N 80∞31E Dikson Krasnoyarsk Krai Russia 72∞47N 56∞09W Upernavik Greenland Denmark 71∞39N 128∞52E Tiksi Sakha Republic Russia 71∞32N 52∞19E Belushya Guba Arkhangelsk Oblast Russia 71∞18N 156∞46W Barrow Alaska United States
But Unicode is already is mess up before this.
Ny-≈lesund should be Ny-Ålesund.
Reply
#7
error traceback indicates:
Error:
'utf-16-le' codec can't decode bytes in position 3692-3693: illegal UTF-16 surrogate
so apparently a codec issue
Reply
#8
These are links to the same file since the links only work once. Im also on Mac if that affects any syntax

https://www.justbeamit.com/aggzt
https://www.justbeamit.com/3wy9h
https://www.justbeamit.com/f9qi9

I still can't figure out how to read the text file. I've tried all the suggestions.
Reply
#9
chardet
E:\div_code\new
λ chardetect cities.txt
cities.txt: Windows-1252 with confidence 0.73
with open('cities.txt', encoding='cp1252') as f:
    print(f.read()[:500])
Output:
Latitude Longitude City Province/State Country 82°30N 62°20W Alert Nunavut Canada 81°36N 16°40W Nord Greenland Denmark 79°59N 85°56W Eureka Nunavut Canada 78°55N 11°56E Ny-Ålesund Svalbard Norway 78°13N 15°39E Longyearbyen Svalbard Norway 77°28N 69°14W Qaanaaq Greenland Denmark 76°25N 82°54W Grise Fiord Nunavut Canada 73°30N 80°31E Dikson Krasnoyarsk Krai Russia 72°47N 56°09W Upernavik Greenland Denmark 71°39N 128°52E Tiksi Sakha Republic Russia 71°32N 52°19E Belushya Guba Arkhangelsk Oblast Rus
Reply
#10
(Sep-27-2018, 03:53 PM)snippsat Wrote: chardet
E:\div_code\new
λ chardetect cities.txt
cities.txt: Windows-1252 with confidence 0.73
with open('cities.txt', encoding='cp1252') as f:
    print(f.read()[:500])
Output:
Latitude Longitude City Province/State Country 82°30N 62°20W Alert Nunavut Canada 81°36N 16°40W Nord Greenland Denmark 79°59N 85°56W Eureka Nunavut Canada 78°55N 11°56E Ny-Ålesund Svalbard Norway 78°13N 15°39E Longyearbyen Svalbard Norway 77°28N 69°14W Qaanaaq Greenland Denmark 76°25N 82°54W Grise Fiord Nunavut Canada 73°30N 80°31E Dikson Krasnoyarsk Krai Russia 72°47N 56°09W Upernavik Greenland Denmark 71°39N 128°52E Tiksi Sakha Republic Russia 71°32N 52°19E Belushya Guba Arkhangelsk Oblast Rus

This works thank you!!!

Do you have an suggestions for reading only specific parts of the file?
ex: extracting only the line containing the city Nord
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 569 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  file handling Newbee question middlecope 2 745 Jan-18-2023, 03:09 PM
Last Post: middlecope
Star python exception handling handling .... with traceback mg24 3 1,219 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  File handling issue GiggsB 4 1,393 Mar-31-2022, 09:35 PM
Last Post: GiggsB
  How can I solve this file handling issue? GiggsB 17 3,421 Feb-14-2022, 04:37 AM
Last Post: GiggsB
  How to solve this file handling issue? GiggsB 3 1,633 Jan-10-2022, 09:36 AM
Last Post: Gribouillis
  File handling knollfinder 3 2,005 Jun-28-2020, 07:39 PM
Last Post: knollfinder
  file handling sivareddy 1 1,602 Feb-23-2020, 07:28 PM
Last Post: jefsummers
  delete file with handling 3Pinter 1 2,071 Oct-17-2019, 04:06 PM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

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