How does Python wrap a file in binary mode?

when scrapy saves data through Pipeline (in txt format), some data "gbk" codec can"t encode character appears as follows.

class TxtPipeline(object):
    def process_item(self,item,spider):
        path=os.getcwd()
        filename = path + "\data\%s.txt"%item["classic"]
        with open(filename, "a")as f:
            f.write(item["title"] + "\n")
            f.write(item["time"] + "\n")
            f.write(item["text"] + "\n")
        ...

so I use the binary append mode to save the data, and unify it into utf8 , such as:

with open(path, "ab")as f:
        f.write(item["title"].encode("utf-8", errors="ignore")+"\n")
   

but"\ n"is not bite, so it is changed to b"\ n", but you can"t change the line to do so.
questions are as follows:

  1. how to solve a problem like the one above?
  2. how to solve the coding problem if is not in binary mode? ( Note: all item entries are strings )

I am not good at learning, so please give me some advice.

Mar.11,2021

with open(path, 'w')as f:

Why not write directly w ? This uft8 can be written directly into the txt file, ah, why use binary?


with open(file,mode='a',encoding='utf-8') as f:

append mode will not delete everything written.
if the character you want to write starts with utf-8 encoding, otherwise you should change the encoding
just specify the encoding when you open the file


you can write them separately

class TxtPipeline(object):
    def process_item(self,item,spider):
        path=os.getcwd()
        filename = path + '\data\%s.txt'%item['classic']
        with open(filename, 'a')as f:
            f.write(item['title'])
            f.write('\n')
        ...
Menu