Python——文件操作,测验7: 文件和数据格式化 (第7周)——Python语言程序设计

文件

 

 

 

 

注意空行!

def csv1():
""" 按行进行倒序排列;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬"""
f = open("data.txt", "r")
s = f.readlines()
s.reverse()
f.close()
f = open("newdata1.txt", "w")
for line in s:
f.write(line)
f.close()
pass


def csv2():
""" 每行数据倒序排列;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬"""
f1 = open("data.txt", "r")
s = f1.readlines()
f1.close()
f2 = open("newdata2.txt", "w")
for line in s:
f2.write(line[::-1])
f2.close()
pass


def csv3():
""" 使用分号(;)代替逗号(,)分割数据,无空格;"""
f1 = open("data.txt", "r")
s = f1.readlines()
f1.close()
f2 = open("newdata3.txt", "w")
for line in s:
t = line.replace(",", ';')
t = t.replace(' ', '')
f2.write(t)
f2.close()
pass


def cvs4():
""" 综合"""
f = open('data.txt', 'r')
lines = f.readlines()
lines.reverse()
f.close()

f2 = open("newdata4.txt", "w")
for line in lines:
line = line.replace(' ', '')
line = line.replace(",", ';')
f2.write(line[::-1])
f2.close()


if __name__ == '__main__':
cvs4()

 

posted @ 2020-04-16 22:39  kekefu  阅读(553)  评论(0编辑  收藏  举报