问题描述
下面的代码在python2.7中可以很好地工作
for thepkg in mypkgs.get('package'):
pkgname = thepkg.get('name').encode('utf-8').replace(' ','_')
print(' <option value="'+pkgname+'">'+pkgname+'</option>')
pkgname = thepkg.get('name').encode('utf-8').replace(' ','_')
TypeError: a bytes-like object is required,not 'str'
我尝试了各种变体,但是当thepkg.get('name')值是非英语字符串(例如日语或中文名称)时,会弹出一个不同的错误,或者无法正确显示呈现的HTML页面。再次在python2.7上的HTML中完美运行
解决方法
此问题是由HTML上的sys.stdout.encoding设置为ANSI_X3.4-1968引起的 当我使用
将编码重置为utf-8时import { useState } from 'react';
export const checkbox = () => {
const array = [
{
id: 0,value: 100,unit: 'gram',price: 20000
},{
id: 1,value: 150,price: 23536
},{
id: 2,value: 590,price: 56700
}
]
const [value,setValue] = useState({});
return (
<div id="checkbox">
{
array.map((data) =>
(
<label>
<span>{data.value} - {data.price}</span>
<input type="radio" defaultChecked={true} onChange={e => setValue(data)} />
</label>
)
)
}
</div>
)
}
在print()之前,HTML中的字符开始显示正常
我也不需要使用.encode('utf-8'),因为默认情况下,pkg.get('name')是一个Unicode字符串