Ad

How To Capture The Value On EOFError With Raw_input In Python 2.7?

- 1 answer

raw data:

k = {u'description': u'First Contentful Paint marks the time at which the first text or image is painted. [Learn more].', u'title': u'First Contentful Paint', u'score': 1.0, u'scoreDisplayMode': u'numeric', u'displayValue': u'0.5\xa0s', u'id': u'first-contentful-paint'}

snippet:

data = k["lighthouseResult"]["audits"]["first-contentful-paint"]["displayValue"]

try:
    val = raw_input(data.encode("utf-8"))
except EOFError:
    print("skipped")

print "output: " + val

In the above snippet, how can I store the results in val; the step is skipped when there is a EOFError on line val = raw_input(data.encode("utf-8"))

Here is the actual output I get, and I am unable to store the values x.x s in a variable

0.5 sskipped

output: 

Expected:

output: 0.5s

Python version: 2.7

Ad

Answer

One way to decode '0.5\xa0s' is to use unicode builtin function to convert this to unicode and tell it to ignore non utf-8 characters, and convert it to string

>>> a = '0.5\xa0s'
>>> str(unicode(a, errors='ignore'))
'0.5s'
Ad
source: stackoverflow.com
Ad