繁体   English   中英

Python readline() 方法导致 UnicodeDecodeError

[英]Python readline() method caused UnicodeDecodeError

我正在尝试读取和提取大型 txt 的信息并将其写入另一个文档,但出现此错误:错误信息 这是我的代码:

#Create list with PLZ, city and state
cepfinal = open("cepfinal.txt", "w")    #file to be written

with open("ceptest2.txt", "r") as fp:   #read file
    while True:
        line = fp.readline()
#   print(str(line))
        x = line.split("\t")            #separate all that have double space
        plz = x[0]                      #extract PLZ
#   print(plz)

        y = x[1]
        mun = y.split("/")              #separe city from state
#   print(mun)
        plzmun = [plz] + mun
#   print(plzmun)
        final = plzmun.pop(2)           #remove state
        plzmun = " ".join(plzmun)       #create string
        print(plzmun)
        cepfinal.write(plzmun + "\n")

fp.close()

这是一个 45 Gb 的文件,所以我想我有一个 memory 问题。 有人可以帮我制作精益代码吗?

你的问题是编码,你可以试试这个来解决你的问题

with  open("ceptest2.txt", "r", encoding="utf8") as fp:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM