在python字符串中处理ascii char [英] Handling ascii char in python string
问题描述
我有一个名称为"SSE-Künden, SSE-Händler.pdf"
的文件,当我在python解释器上打印此文件名时,该文件具有这两个unicode char ( ü,ä)
,unicode值已转换为相应的ascii值,我猜是'SSE-K\x81nden, SSE-H\x84ndler.pdf'
,但我想
i have file having name "SSE-Künden, SSE-Händler.pdf"
which having those two unicode char ( ü,ä)
when i am printing this file name on python interpreter the unicode values are getting converted into respective ascii value i guess 'SSE-K\x81nden, SSE-H\x84ndler.pdf'
but i want to
测试目录包含名称为SSE-Künden,SSE-Händler.pdf"的pdf文件.
test dir contains the pdf file of name 'SSE-Künden, SSE-Händler.pdf'
我尝试了这个: 路径='C:\ test' 对于os.walk(path)中的a,b,c: 打印c
i tried this: path = 'C:\test' for a,b,c in os.walk(path): print c
['SSE-K\x81nden, SSE-H\x84ndler.pdf']
我该如何将ascii字符转换为各自的unival val,我想在解释器上显示原始名称("SSE-Künden, SSE-Händler.pdf"
),并按原样写入某些文件.我如何实现这一点.我正在使用Python 2.6和Windows OS.
how do i convert this ascii chars to its respective unicode vals and i want to show the original name("SSE-Künden, SSE-Händler.pdf"
) on interpreter and also writeing into some file as it is.how do i achive this. I am using Python 2.6 and windows OS.
谢谢.
推荐答案
假设您的终端支持显示字符,请遍历文件列表并单独打印(或使用Python 3,它在列表中显示Unicode):>
Assuming your terminal supports displaying the characters, iterate over the list of files and print them individually (or use Python 3, which displays Unicode in lists):
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> for p,d,f in os.walk(u'.'):
... for n in f:
... print n
...
SSE-Künden, SSE-Händler.pdf
还请注意,我为路径使用了Unicode字符串(u'.').这指示os.walk
返回Unicode字符串而不是字节字符串.在处理非ASCII文件名时,这是一个好主意.
Also note I used a Unicode string (u'.') for the path. This instructs os.walk
to return Unicode strings as opposed to byte strings. When dealing with non-ASCII filenames this is a good idea.
在Python 3中,默认情况下,字符串为Unicode,并且向用户显示非ASCII字符,而不是将其显示为转义码:
In Python 3 strings are Unicode by default and non-ASCII characters are displayed to the user instead of displayed as escape codes:
Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> for p,d,f in os.walk('.'):
... print(f)
...
['SSE-Künden, SSE-Händler.pdf']
这篇关于在python字符串中处理ascii char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!