python中向文本每行行尾添加字符

python中向文本每行行尾添加字符,第1张

def read_file(file_path="abc.txt"):

file = open(file_path, "r")

content = file.readlines()

file.close()

return content

def write_file(content, file_path="abc1.txt"):

file = open(file_path, "w")

file.writelines(content)

file.flush()

file.close()

if __name__ == "__main__":

content = read_file()

new_content = list()

for c in content:

c = c.strip()

new_content.append(c + str(int(c) - 1) + "\r\n")

write_file(new_content)

s="指定字符串"f=open("文本文件名")n=int(raw_input("输入输出的行数:"))line=f.readline()while line:if s in line:print linefor i in range(n):line=f.readline()if line:print lineelse:breakif not line:breakline=f.readline()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11456835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存