简体   繁体   English

Python readline() 方法导致 UnicodeDecodeError

[英]Python readline() method caused UnicodeDecodeError

I`m trying to read and extract information of a large txt and to write it in another document, and I get this error:我正在尝试读取和提取大型 txt 的信息并将其写入另一个文档,但出现此错误:错误信息 Here is my code:这是我的代码:

#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()

It is a 45 Gb file, so I suppose I have a memory issue.这是一个 45 Gb 的文件,所以我想我有一个 memory 问题。 Can someone help me to make a lean code?有人可以帮我制作精益代码吗?

your problem is with encoding, you can try this to solve your problem你的问题是编码,你可以试试这个来解决你的问题

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

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

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