草庐IT

unicode_literals

全部标签

python - 如何让 str.translate 使用 Unicode 字符串?

我有以下代码:importstringdeftranslate_non_alphanumerics(to_translate,translate_to='_'):not_letters_or_digits=u'!"#%\'()*+,-./:;?@[\]^_`{|}~'translate_table=string.maketrans(not_letters_or_digits,translate_to*len(not_letters_or_digits))returnto_translate.translate(translate_table)这对非unicode字符串非常有效:>>>t

python - 如何让 str.translate 使用 Unicode 字符串?

我有以下代码:importstringdeftranslate_non_alphanumerics(to_translate,translate_to='_'):not_letters_or_digits=u'!"#%\'()*+,-./:;?@[\]^_`{|}~'translate_table=string.maketrans(not_letters_or_digits,translate_to*len(not_letters_or_digits))returnto_translate.translate(translate_table)这对非unicode字符串非常有效:>>>t

python - TypeError:强制转换为 Unicode:需要字符串或缓冲区

此代码返回以下错误消息:open(infile,mode='r',buffering=-1)asin_f,open(outfile,mode='w',buffering=-1)asout_f:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件#Openseachfiletoread/modifyinfile=open('110331_HS1A_1_rtTA.result','r')outfile=open('2.txt','w')importrewithopen(infile,mode='r',buffering=-1)asin_f,open(outfile,m

python - TypeError:强制转换为 Unicode:需要字符串或缓冲区

此代码返回以下错误消息:open(infile,mode='r',buffering=-1)asin_f,open(outfile,mode='w',buffering=-1)asout_f:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到文件#Openseachfiletoread/modifyinfile=open('110331_HS1A_1_rtTA.result','r')outfile=open('2.txt','w')importrewithopen(infile,mode='r',buffering=-1)asin_f,open(outfile,m

python - 值错误 : invalid literal for int () with base 10

我写了一个程序来解决y=a^x然后将其投影到图表上。问题是每当a我得到错误:ValueError:invalidliteralforint()withbase10.有什么建议吗?这是回溯:Traceback(mostrecentcalllast):File"C:\Users\kasutaja\Desktop\EksponentfunktsioonTEST-koopia.py",line13,inifint(a)每次我输入一个小于1但大于0的数字时都会出现问题。对于本示例,它是0.3。这是我的代码:#y=a^ximporttimeimportmathimportsysimportosim

python - 值错误 : invalid literal for int () with base 10

我写了一个程序来解决y=a^x然后将其投影到图表上。问题是每当a我得到错误:ValueError:invalidliteralforint()withbase10.有什么建议吗?这是回溯:Traceback(mostrecentcalllast):File"C:\Users\kasutaja\Desktop\EksponentfunktsioonTEST-koopia.py",line13,inifint(a)每次我输入一个小于1但大于0的数字时都会出现问题。对于本示例,它是0.3。这是我的代码:#y=a^ximporttimeimportmathimportsysimportosim

python - Python 中 unicode() 和 encode() 函数的使用

我在编码path变量并将其插入到SQLite数据库时遇到问题。我试图用encode("utf-8")函数解决它,但没有帮助。然后我使用了unicode()函数,它给了我类型unicode。printtype(path)#path=path.replace("one","two")#path=path.encode("utf-8")#strangepath=unicode(path)#最后我获得了unicode类型,但是当path变量的类型为strsqlite3.ProgrammingError:Youmustnotuse8-bitbytestringsunlessyouuseatext

python - Python 中 unicode() 和 encode() 函数的使用

我在编码path变量并将其插入到SQLite数据库时遇到问题。我试图用encode("utf-8")函数解决它,但没有帮助。然后我使用了unicode()函数,它给了我类型unicode。printtype(path)#path=path.replace("one","two")#path=path.encode("utf-8")#strangepath=unicode(path)#最后我获得了unicode类型,但是当path变量的类型为strsqlite3.ProgrammingError:Youmustnotuse8-bitbytestringsunlessyouuseatext

Python 和带有 Unicode 的正则表达式

我需要从字符串'بِسْمِاللَّهِالرَّحْمَٰنِالرَّحِيمِ'中删除一些Unicode符号我知道它们肯定存在于此。我试过了:re.sub('([\u064B-\u0652\u06D4\u0670\u0674\u06D5-\u06ED]+)','','بِسْمِاللَّهِالرَّحْمَٰنِالرَّحِيمِ')但它不起作用。字符串保持不变。我做错了什么? 最佳答案 您使用的是python2.x还是3.0?如果您使用的是2.x,请尝试使用'u'将正则表达式字符串设为unicode-escape字符

Python 和带有 Unicode 的正则表达式

我需要从字符串'بِسْمِاللَّهِالرَّحْمَٰنِالرَّحِيمِ'中删除一些Unicode符号我知道它们肯定存在于此。我试过了:re.sub('([\u064B-\u0652\u06D4\u0670\u0674\u06D5-\u06ED]+)','','بِسْمِاللَّهِالرَّحْمَٰنِالرَّحِيمِ')但它不起作用。字符串保持不变。我做错了什么? 最佳答案 您使用的是python2.x还是3.0?如果您使用的是2.x,请尝试使用'u'将正则表达式字符串设为unicode-escape字符