zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    day_15补充加总结
    Day_15 内置模块er
    sort 函数 与 sorted 函数区别
    python 代码风格------------PEP8规则
    python 返回值
    函数记忆 : startswith() 与 endswith
    RE模块使用
    正则表达式
    collections
    函数datetime
  • 原文地址:https://www.cnblogs.com/kekefu/p/12716423.html
Copyright © 2011-2022 走看看