0

I want to get the responses from the api. But I have a problem about ElementTree Unicode. I can't encode the korean words in my code. And I try to follow this link but still not work. python requests.get() returns improperly decoded text instead of UTF-8? I added res.encoding = ('utf-8') but still got the same error. If my error is about this link, how can I change UTF-8 instead of ISO-8859-1?

test.csv:

Email_Address
ㅈㄴㄷㄱ닏ㄱㅎ

my code:

# -*- coding: utf-8 -*-
import requests
from dicttoxml import dicttoxml
from xml.etree import ElementTree
import csv
import re
import pandas as pd
import json
from collections import defaultdict

def etree_to_dict(t):
    with io.StringIO() as string:
        string.write(ET.tostring(t.root, encoding="unicode"))
    d = {t.tag: {} if t.attrib else None}
    children = list(t)
    if children:
        dd = defaultdict(list)
        for dc in map(etree_to_dict, children):
            for k, v in dc.items():
                dd[k].append(v)
        d = {t.tag: {k:v[0] if len(v) == 1 else v for k, v in dd.items()}}
    if t.attrib:
        d[t.tag].update(('@' + k, v) for k, v in t.attrib.items())
    if t.text:
        text = t.text.strip()
        if children or t.attrib:
            if text:
              d[t.tag]['#text'] = text
        else:
            d[t.tag] = text
    return d

# ---
df = pd.read_csv('test.csv',delimiter = '|',encoding='utf-8',dtype = str)

responses = []

for index, row in df.iterrows():
    res = requests.post("0.0.0.0:5000/EmailValidation/api/validation/",
        data=dicttoxml({
            'Username': 'Username',
            'Password': 'Password',
            'Email': row["Email_Address"],
            'FIX-TYPO': 'TRUE',
        }, custom_root='ValidationRequest', attr_type=False),
        headers={'Content-Type': 'application/xml'}
    )
    res.encoding = ('utf-8')
    root = ElementTree.XML(res.text)

    responses.append(etree_to_dict(root)['ValidationResponse']['ResponseEmail'])
    
    # print(etree_to_dict(root)['ValidationResponse']['ResponseEmail'])
df = pd.read_json(json.dumps(responses))

print(df)

error:

Traceback (most recent call last):
  File "callapi_xml.py", line 47, in <module>
    root = ElementTree.XML(res.text)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML
    parser.feed(text)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1651, in feed
    self._parser.Parse(data, 0)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 66-72: ordinal not in range(128)
hang
  • 25
  • 6

0 Answers0