Ad
How To Capture The Value On EOFError With Raw_input In Python 2.7?
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
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad