def full_to_half(full_width_string):
fullString = str(full_width_string)
halfString = ""
for schar in fullString:
char_code = ord(schar)
if char_code == 12288:
char_code=32
elif (char_code>=65281 and char_code<=65374):
char_code -= 65248
halfString += chr(char_code)
return halfString
strings = "string今天下大暴雨,!?老师教了+和-,乘法×,除法。Lucky Day! 这道题我也会做,哈哈哈。"
full_to_half(strings)
Out[12]: 'string今天下大暴雨,!?老师教了+和-,乘法×,除法。Lucky Day! 这道题我也会做,哈哈哈。'
此处使用chr进行转换计算,有的文章中使用了 from inda import unichr,在Python 3中运行时会以下错误
ImportError: cannot import name 'unichr' from 'idna' (D:\ProgramFiles\anaconda3\lib\site-packages\idna\__init__.py)
这是因为在在Python 3中unicode和普通字符串之间没有任何区别。仅在unicode字符串和二进制数据之间。因此开发人员最终取消了unichr功能,转而使用了共同的chr,现在可以完成旧的unichr