如何转换的文件名由UNI code到ASCII [英] How do I convert filenames from unicode to ascii

查看:126
本文介绍了如何转换的文件名由UNI code到ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须安装在与UNI code字符的文件名的Linux NTFS分区一堆音乐文件。我无法写一个脚本,以便所有的文件名中仅使用ASCII字符重命名文件。我觉得用的iconv 命令应该工作,但我无法逃避的字符MV 命令。

I have a bunch of music files on a NTFS partition mounted on linux that have filenames with unicode characters. I'm having trouble writing a script to rename the files so that all of the file names use only ASCII characters. I think that using the iconv command should work, but I'm having trouble escaping the characters for the 'mv' command.

编辑:如果没有对UNI code字符直接translieration不要紧。我想,我会只替换那些带有?性格。

It doesn't matter if there isn't a direct translieration for the unicode chars. I guess that i'll just replace those with a "?" character.

推荐答案

我不认为的iconv 有任何字符替代设施。这在Python可以帮助:

I don't think iconv has any character replacement facilities. This in Python might help:

#!/usr/bin/python
import sys

def unistrip(s):
    if isinstance(s, str):
        s = s.decode('utf-8')
    chars = []
    for i in s:
        if ord(i) > 0x7f:
            chars.append(u'?')
        else:
            chars.append(i)
    return u''.join(chars)

if __name__ == '__main__':
    print unistrip(sys.argv[1])

然后当拨打:

这篇关于如何转换的文件名由UNI code到ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆