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


Messages In This Thread
Help with file handling - by gonzo620 - Sep-26-2018, 11:33 PM
RE: Help with file handling - by stullis - Sep-27-2018, 01:27 AM
RE: Help with file handling - by Larz60+ - Sep-27-2018, 01:31 AM
RE: Help with file handling - by gonzo620 - Sep-27-2018, 03:41 AM
RE: Help with file handling - by Larz60+ - Sep-27-2018, 11:52 AM
RE: Help with file handling - by snippsat - Sep-27-2018, 02:07 PM
RE: Help with file handling - by Larz60+ - Sep-27-2018, 03:34 PM
RE: Help with file handling - by gonzo620 - Sep-27-2018, 03:42 PM
RE: Help with file handling - by snippsat - Sep-27-2018, 03:53 PM
RE: Help with file handling - by gonzo620 - Sep-27-2018, 05:50 PM
RE: Help with file handling - by nilamo - Sep-27-2018, 06:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 864 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  file handling Newbee question middlecope 2 826 Jan-18-2023, 03:09 PM
Last Post: middlecope
Star python exception handling handling .... with traceback mg24 3 1,327 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  File handling issue GiggsB 4 1,488 Mar-31-2022, 09:35 PM
Last Post: GiggsB
  How can I solve this file handling issue? GiggsB 17 3,723 Feb-14-2022, 04:37 AM
Last Post: GiggsB
  How to solve this file handling issue? GiggsB 3 1,739 Jan-10-2022, 09:36 AM
Last Post: Gribouillis
  File handling knollfinder 3 2,095 Jun-28-2020, 07:39 PM
Last Post: knollfinder
  file handling sivareddy 1 1,663 Feb-23-2020, 07:28 PM
Last Post: jefsummers
  delete file with handling 3Pinter 1 2,128 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