处理Python字符串中的ascii字符

发布于 2024-12-06 01:42:21 字数 565 浏览 0 评论 0原文

当我在 python 上打印此文件名时,我有一个名为 "SSE-Künden, SSE-Händler.pdf" 的文件,其中包含这两个 unicode char ( ü,ä)解释器将 unicode 值转换为相应的 ascii 值,我猜 'SSE-K\x81nden, SSE-H\x84ndler.pdf' 但我想

测试目录包含名为“SSE-Künden,SSE-Händler.pdf”的 pdf 文件

我尝试过: 路径 = 'C:\test' 对于 os.walk(path) 中的 a、b、c: print c

['SSE-K\x81nden, SSE-H\x84ndler.pdf']

我如何将此 ascii 字符转换为其各自的 unicode 值,我想在解释器上显示原始名称(“SSE-Künden,SSE-Händler.pdf”)并写入某个文件就这样。我该如何实现这一目标。我正在使用 Python 2.6 和 Windows 操作系统。

谢谢。

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

test dir contains the pdf file of name 'SSE-Künden, SSE-Händler.pdf'

i tried this:
path = 'C:\test'
for a,b,c in os.walk(path):
print c

['SSE-K\x81nden, SSE-H\x84ndler.pdf']

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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼波传意 2024-12-13 01:42:21

假设您的终端支持显示字符,请迭代文件列表并单独打印它们(或使用 Python 3,它在列表中显示 Unicode):

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 文件名时,这是一个好主意。

在 Python 3 中,字符串默认为 Unicode,并向用户显示非 ASCII 字符,而不是显示为转义码:

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']

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

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.

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']
2024-12-13 01:42:21
for a,b,c in os.walk(path):
    for n in c:
        print n.decode('utf-8')
for a,b,c in os.walk(path):
    for n in c:
        print n.decode('utf-8')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文