【问题标题】:Python: UTF-8 german special charsPython:UTF-8 德语特殊字符
【发布时间】:2014-03-25 03:37:55
【问题描述】:

我正在 python 脚本中搜索文件并存储文件路径。 问题是,在某些情况下会有特殊的字符,比如 ö ä ü 内部(UTF-8 表十六进制 U+00C4 U+00D6 U+00DC 等) 当我用“打印”打印路径时,它会正确显示。当我使用这个 用于将其发送到 os.system() 的字符串,特殊字符被转义并 收到 UTF 错误。

错误消息:

cp -nv /home/rainer/Arbeitsfläche/Videofiles/A047C001_130226_R1WV.mov /media/rainer/LinuxData
Traceback (most recent call last):
  File "Clipfinder.py", line 254, in <module>
    copyProcess(sourcedir,destdir,cliplist)
  File "Clipfinder.py", line 205, in copyProcess
    os.system(copycmd)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 29: ordinal not in range(128)

谢谢帮助! 雨儿

copycmd = "cp -nv " + pathtoFile_src + " " + destdir
print copycmd
os.system(copycmd)

【问题讨论】:

    标签: python utf


    【解决方案1】:

    使用encode将unicode转换为字节串:

    os.system(copycmd.encode('utf-8'))
    

    【讨论】:

    • 太棒了!谢谢你的提示!!!它现在可以工作了...忘记了里面的旧命令,所以我又收到了错误...现在很好!!!!
    • 现在是处理空白字符。 cp 正在切断那里的命令。
    • 我正在使用可以正常工作的替换功能来转义白字符。但我现在有另一个问题。当我将带有特殊字符的字符串添加到列表中时出现错误:扫描源目录 /home/rainer/Arbeitsfläche/Videofiles 测试文件。这可能需要一些时间!回溯(最后一次调用):文件“Clipfinder_RAW.py”,第 346 行,在 copyProcess(sourcedir,destdir,cliplist) 文件“Clipfinder_RAW.py”,第 238 行,在 copyProcess found_srcdirs.encode('utf-8 ') UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 22: ordinal not in range(128)
    • 在将其写入列表之前:/home/rainer/Arbeitsfläche/Videofiles test/A051R1WV/A051C001_130227_R1WV.mov 然后当它在列表中时:'/home/rainer/Arbeitsfl\xc3\xa4che/Videofiles测试/A051R1WV'
    • @rainer 你应该在编码之前解码字符串(你已经有编码的字节字符串):print '/home/rainer/Arbeitsfl\xc3\xa4che/Videofiles test/A051R1WV'.decode('utf-8') /home/rainer/Arbeitsfläche/Videofiles test/A051R1WV
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 2012-04-16
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多