python3 读写文件换行符

最近在处理文本文件时,遇到编码格式和换行符的问题。

基本上都是GBK 和 UTF-8 编码的文本文件,但是python3 中默认的都是按照 utf-8 来打开。用不正确的编码参数打开,在读取内容时,会抛出异常。

open(dirpath + "\\" + file, mode = "r+", encoding = "gbk", newline = "")

捕获抛出的异常,关闭文件。使用另外一种编码格式打开文件再重新读取。

读取文件时,

newline参数用来指定读取时,对换行符的处理。缺省为 None,表示通用的换行符(“\n”),即文件的换行符是啥,读出来都是 “\n”.

newline = ""  表示读取的换行符保持不变,原来是啥,读出来还是啥。

newline = “\n”  表示遇到 "\n" 才一行结束,“\r” 像其他普通字符一样对待。

newline = “\r”  表示遇到 "\r" 才一行结束,“\n” 像其他普通字符一样对待。


在文件写入时,

newline = None时,写入的“\n” 自动都变为系统默认的换行符。所以 “\r\n” 在windows下会变成“\r\r\n”写入。

newline = ""  表示不做任何转换写入。

newline = “\n”  表示不做任何转换写入。

newline = “\r”  表示将 “\n” 和 "\r" 都当做 "\r" 进行写入,所以“\r\n” 会变成 “\r\r”进行写入。


案例:将源码下的所有makefile 文件中的 -c 参数前,加上 -g 选项。

import os
import re

os.chdir(r"E:\code")
s = os.walk(".")
pattern = re.compile(r"\s-c\s")
for dirpath, dirnames, filenames in s:
	for file in filenames:
		if file.endswith(".mak") or "makefile" in file:    #部分以 .mak 结尾,部分以makefile命名
			print(file)
			with open(dirpath + "\\" + file, mode = "r+", encoding = "gbk", newline = "") as f:  #newline为空串表示换行符不转换
				try:    #编码问题造成的异常
					lines = f.readlines()  #一次读取所有的行到内存
					f.seek(0)               #回到文件起始处
					for line in lines:
						#newline = line.replace(" -c "," -g -c ")
						newline= re.sub(pattern, " -g -c ", line)
						f.write(newline)
				except ValueError:
					f.close()
					with open(dirpath + "\\" + file, mode = "r+", encoding = "utf-8", newline = "") as fnew:
						try:    
							lines = fnew.readlines()  
							fnew.seek(0)             
							for line in lines:
								#newline = line.replace(" -c "," -g -c ")
								newline= re.sub(pattern, " -g -c ", line)
								fnew.write(newline)
						except ValueError:
							print("***************  " + dirpath + "\\" + file)    #打印utf-8 和 gbk 之外编码的文件名



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值