python - 如何在 Python 2.7 中使用 raw_input 捕获 EOFError 的值?
问题描述
原始数据:
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'}
片段:
data = k["lighthouseResult"]["audits"]["first-contentful-paint"]["displayValue"]
try:
val = raw_input(data.encode("utf-8"))
except EOFError:
print("skipped")
print "output: " + val
在上面的代码片段中,我如何将结果存储在val
; EOFError
在线时跳过该步骤val = raw_input(data.encode("utf-8"))
这是我得到的实际输出,我无法将值存储x.x s
在变量中
0.5 sskipped
output:
预期的:
output: 0.5s
蟒蛇版本:2.7
解决方案
一种解码方法'0.5\xa0s'
是使用unicode内置函数将其转换为 unicode 并告诉它忽略非utf-8
字符,并将其转换为字符串
>>> a = '0.5\xa0s'
>>> str(unicode(a, errors='ignore'))
'0.5s'
推荐阅读
- android - 在 Inntellij 的 DataSoruce 中找不到 Android SQLite
- c# - 使用字符串 ID 时未找到视图
- powershell - 记录某事的最佳 PowerShell 动词?
- aws-cli - AWS CLI V2“AWS firehose put-record”抱怨无效的 base64:
- flutter - Flutter - SliverAppBar - 如何在用户开始向上滚动后立即显示 AppBar
- javascript - React:导入本地图像时发现404
- python - 如何计算随机抛硬币产生的“正面”数量和“反面”数量?
- jquery - Puppeteer 中的选择器语法
- python - Discord.py - 将角色移得更高
- canvas - 如何控制画布在文档中的放置位置?