Advertisement

Python处理含有中文的txt文件读取

阅读量:

参考:python读取、写入txt文本内容

必须包含encoding='utf8'参数

复制代码
    with open("test.txt", 'r', encoding='utf8') as f:
    	# 以下为每一行单独存放在一个str,info为一个list
    infor = f.readlines()
    # 也可以全部存放在一个str里面
    # infor = f.read()
    
    
      
      
      
      
      
    

在进行写入操作时,无需特别添加encoding='utf8'参数。

复制代码
    with open("test.txt","w") as f:
    f.write("这是个测试!")  
    
    
      
      
    

通用范式

复制代码
    def read_txt(txt_name):
    with open(txt_name, 'r', encoding='utf-8') as f:
        txt_list = f.readlines()
    for i in range(len(txt_list)):
        txt_list[i] = txt_list[i].

全部评论 (0)

还没有任何评论哟~