先看效果
思路:将python逐行读取存入列表,然后换行输出
逐行读取效果 从input.txt到命令行输出列表
换行输出效果 从列表输出到ouput.txt
示例数据
contentType.put(".load" , "text/html"); contentType.put(".123" , "application/vnd.lotus-1-2-3"); contentType.put(".3ds" , "image/x-3ds"); contentType.put(".3g2" , "video/3gpp"); contentType.put(".3ga" , "video/3gpp"); contentType.put(".3gp" , "video/3gpp"); contentType.put(".3gpp" , "video/3gpp"); contentType.put(".602" , "application/x-t602"); contentType.put(".669" , "audio/x-mod"); contentType.put(".7z" , "application/x-7z-compressed"); contentType.put(".a" , "application/x-archive"); contentType.put(".aac" , "audio/mp4");
六行代码
#逐行读取 f=open('input.txt', encoding='gbk') list=[] [list.append(line.strip()) for line in f] # print(list) # line.strip() 去除首尾空格 #换行输出 with open('ouput.txt','w') as f: [f.write(str(content)+'\n') for content in list] f.close