0

My first post :o) Also my first python steps. I want to fetch data from an xml api and searched for some youtube videos. I found this one: https://youtu.be/jeZp5NE9lII?t=685 (XML part)

I "copied" the code from the video (where the python script runs) to found out if I can fetch informations from an XML API or if I need to add something to my ATOM or Python. Here the code:

import requests
import xml.etree.ElementTree as ET

url = 'http://api.worldbank.org/countries'

response = requests.get(url)
country_data = response.text

countries = ET.fromstring(country_data)

namespaces = {'wb': 'http://www.worldbank.org'}

for country in countries.findall('wb:country', namespaces):
    name = country.find('wb:name', namespaces).text
    code = country.find('wb:iso2Code', namespaces).text

print('country: {} {}'.format(name, code))

Had some errors. So I installed on my mac:

  1. sudo easy_install pip
  2. python -m pip install requests

Now I have the following error:

  File "/var/folders/2k/ynjbnhl17pg6_wtr20l149xh0000gn/T/atom_script_tempfiles/3a3bf940-16b6-11eb-8d53-6b13b4204efb", line 30, in <module>
    countries = ET.fromstring(country_data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML
    parser.feed(text)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1657, in feed
    self._parser.Parse(data, 0)
UnicodeEncodeError: 'ascii' codec can't encode```

What I did wrong? 
3
  • 1
    Do you have a really, really good reason to be writing in Python 2.7? As a beginner you should not be investing time in learning it. One of the major changes in Python 3 is that its Unicode strings do away with many mystifying encoding and decoding challenges. Commented Oct 25, 2020 at 12:03
  • Thanks for your hint. No there is no reason why I use Python 2.7. Actually I had Python 3.9 installed. According to my research, Atom goes to the standard version of Mac OS = Python 2.7, so I have Python 2 and 3 on the Mac co-exist: stackoverflow.com/questions/35546627/…. So I had to set the right Python Version in Atom: After that the error message changed that I did not install Requests ;) Commented Oct 25, 2020 at 15:43
  • That is an expected message. Installing a module in one Python environment does net make it available in another. The code could well be different. So after changing to Python 3, go to your Python 2 environment, and do pip list to find out what installs you need to do in Python 3 to replicate what you have in Python 2. Commented Oct 25, 2020 at 16:04

1 Answer 1

1

If you print the first 50 characters of response.text, you get this:

<?xml version="1.0" encoding="utf-8"?>
<wb:cou

The strange looking characters at the beginning are a Byte Order Mark, and their presence suggests that requests hasn't decoded the file correctly. Instead of using response.text, use response.content to pass the raw bytes to elementtree:

 country_data = response.content

Resulting in this output

country: China CN
Sign up to request clarification or add additional context in comments.

1 Comment

You may find it easier to work with different encodings if you use Python 3 rather than Python 2.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.