首页 > 编程 > Python > 正文

python3 读写文件换行符的方法

2020-02-22 23:38:58
字体:
来源:转载
供稿:网友

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

基本上都是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 osimport reos.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 之外编码的文件名

以上这篇python3 读写文件换行符的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林站长站。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表