美文网首页
Python--读取含有中文的TXT

Python--读取含有中文的TXT

作者: WYH0804 | 来源:发表于2016-08-20 16:29 被阅读0次

    方法一:

    import codecs

    with codecs.open('somefile.txt', 'r', 'GBK') as f:
    data = f.read()
    print data


    方法二:

    import sys
    reload(sys)
    sys.setdefaultencoding("utf-8")

    fin = open('somefile.txt', 'r')
    for eachLine in fin:
    line = eachLine.strip().decode('gbk', 'utf-8')
    print line

    相关文章

      网友评论

          本文标题:Python--读取含有中文的TXT

          本文链接:https://www.haomeiwen.com/subject/gesisttx.html