Forums

encode in pythonanywhere

I want to know if # -- using utf8 -- is the same in PA? iwant to put after #!/usr/bin/python

I am facing some error ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters which I want to fix

I try to declare with # coding: utf-8 also not working. I am suing Flask

The comment to set the coding should be

# -*- coding: utf-8 -*-

...on a new line after the initial hashbang line -- note the dashes and stars. Could you try that and see what happens?

Hi giles,

It does not work. It works on my local pc though but fail in PA with the error shown in the first post.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/python
# -*- coding: utf-8 -*-
print "hello from python 2"
# A very simple Flask Hello World app for you to get started with...

@app.route('/thtop', methods=['GET'])
@app.cache.cached(timeout=60)
def thtop():
    page_th = requests.get('http://www.settrade.com/C13_MarketSummary.jsp?order=Y')
    th = html.fromstring(page_th.content)
    a= th.xpath('//div[@class="divDetailBox" and .//h3[.="มูลค่าซื้อขาย 20 อันดับ"]]//tr/td/text()')
    return "okay"

I don't know when I move to PA, it just repeated the ASCII error.

In line 11 the argument to xpath needs to be a Unicode literal, so prefix with u.

a = xpath(u'//div[...

What is the full error stacktrace btw? If it's line 11, aren't you just trying to give it unicode characters while xpath is saying it doesn't accept unicode characters? "ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters"

Hi conrad,

u' eliminate the error.

However, I still find that the Thai do not take effect. No matter I change the Thai words the output of xpath still the same

What's the return value that you get from the th.xpath function?