Python Forum
Right way to write string into UTF-8 file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Right way to write string into UTF-8 file?
#4
You don't have encoding='utf-8' when you read the file as i show.
Test with your file.
with open('input.gpx') as f:
    print(f.read())
Output:
<?xml version="1.0" encoding="UTF-8"?> <desc>Départ entre 7 et 8h</desc> </gpx>
Fix:
with open('input.gpx', encoding='utf-8') as f:
    print(f.read())
Output:
<?xml version="1.0" encoding="UTF-8"?> <desc>Départ entre 7 et 8h</desc> </gpx>
As it's a .xml file,BS parser test.
from bs4 import BeautifulSoup

soup = BeautifulSoup(open('input.gpx', encoding='utf-8'), 'xml')
print(soup.find('desc').text)
Output:
Départ entre 7 et 8h
Reply


Messages In This Thread
RE: Right way to write string into UTF-8 file? - by snippsat - Aug-28-2018, 05:31 PM

Forum Jump:

User Panel Messages

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