Python unicode错误 [英] Python unicode error

查看:65
本文介绍了Python unicode错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用python 2构建的python程序,但是现在我必须重建它,并且已经将某些内容更改为python3,但是不知何故,我的csv没有被加载并说...

I have a program made in python that was built to python 2 , but now i have to rebuild it and have already change some things to python3 but somehow, my csv is not being loaded and says ...

第一个示例的未解析参考unicode(我已经在这里看到了一个解决方案,但是根本没有用)并说有未解决的参考文件,有人可以提前帮助我吗?)

Unresolved reference unicode for the first example ( i already seen a solution here but it didn't worked at all ) And says unresolved reference file, can anybody help me plz thks in advance ;)

 def load(self, filename):

    try:
        f = open(filename, "rb")
        reader = csv.reader(f)
        for sub, pre, obj in reader:
            sub = unicode(sub, "UTF-8").encode("UTF-8")
            pre = unicode(pre, "UTF-8").encode("UTF-8")
            obj = unicode(obj, "UTF-8").encode("UTF-8")
            self.add(sub, pre, obj)
        f.close()
        print
        "Loaded data from " + filename + " !"

    except:
        print
        "Error opening file!"

def save(self, filename):
    fnm = filename ;
    f = open(filename, "wb")
    writer = csv.writer(f)
    for sub, pre, obj in self.triples(None, None, None):
        writer.writerow([sub.encode("UTF-8"), pre.encode("UTF-8"), obj.encode("UTF-8")])
    f.close()

    print
    "Written to " + filename

推荐答案

unicode(sub, "UTF-8")

应该是

sub.decode("UTF-8")

Python3统一了 str unicode 类型,因此不再有内置的 unicode 强制转换运算符.

Python3 unified the str and unicode types so there's no longer a builtin unicode cast operator.

Python 3 Unicode HOWTO 解释很多差异.

The Python 3 Unicode HOWTO explains a lot of the differences.

从Python 3.0开始,该语言的特征是包含Unicode字符的str类型,这意味着使用"unicode rock!" 'unicode rock!'或三引号字符串语法存储为Unicode.

Since Python 3.0, the language features a str type that contain Unicode characters, meaning any string created using "unicode rocks!", 'unicode rocks!', or the triple-quoted string syntax is stored as Unicode.

并说明 encode decode 如何相互关联

转换为字节

bytes.decode()的相反方法是 str.encode(),它返回Unicode字符串的 bytes 表示形式,编码为请求的编码.

Converting to Bytes

The opposite method of bytes.decode() is str.encode(), which returns a bytes representation of the Unicode string, encoded in the requested encoding.


代替

file(...)

使用 open

I/O文档解释如何使用 open 以及如何使用 with 来确保将其关闭.

The I/O docs explain how to use open and how to use with to make sure it gets closed.

在处理文件对象时,最好使用with关键字.这样做的好处是,即使在执行过程中引发了异常,文件在其套件完成后也将被正确关闭.它也比编写等效的try-finally块要短得多:

It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks:

 >>> with open('workfile', 'r') as f:
 ...     read_data = f.read()
 >>> f.closed
 True

这篇关于Python unicode错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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