• csv文件读取与写入


    
    
    ZQDM,RQ,TGL,LTCSDM
    XS1529757830,2016/12/20,300000000,50
    XS1580430681,2017/03/20,500000000,50
    XS1580430681,2019/03/07,1600000000,50
    XS1991398352,2019/04/23,0,50
    XS1991398352,2019/05/06,172333000,50
    XS1991398352,2019/05/08,372333000,50






    #!/usr/bin/python3
    # _*_ coding:utf-8 _*_
    import datetime

    def get_Every_Day(begin_date, end_date):
    date_list = []
    begin_date = datetime.datetime.strptime(begin_date, "%Y/%m/%d")
    end_date = datetime.datetime.strptime(end_date, "%Y/%m/%d")
    while begin_date <= end_date:
    date_str = begin_date.strftime("%Y/%m/%d")
    date_list.append(date_str)
    begin_date += datetime.timedelta(days=1)
    return date_list


    def add_data(file_in, end_date):
    file_out = ''.join(file_in.strip().split('.')[0:-1])+'_clean.'+file_in.strip().split('.')[-1]
    print(file_in)
    deposit_in = open(file_in, 'r')
    clean_out = open(file_out, 'w')
    deposit_all_lines = deposit_in.readlines()
    deposit_in.close()
    clean_out.write(deposit_all_lines[0])
    clean_out.write(deposit_all_lines[1])
    for i in range(2, len(deposit_all_lines)-1):
    line1 = deposit_all_lines[i].strip().split(',')
    print(line1)
    name1 = line1[0]
    date1 = line1[1]
    money1 = line1[2]
    if money1 == '0':
    print(deposit_all_lines[i].strip())
    continue
    loc1 = line1[3]

    line2 = deposit_all_lines[i+1].strip().split(',')
    name2 = line2[0]
    date2 = line2[1]

    if name1 == name2:
    date_list = get_Every_Day(date1, date2)
    for j in range(0, len(date_list)-1):
    clean_out.write(name1+','+date_list[j]+','+money1+','+loc1+' ')
    else:
    date_list = get_Every_Day(date1, end_date)
    for j in range(0, len(date_list)):
    clean_out.write(name1+','+date_list[j]+','+money1+','+loc1+' ')

    line_end = deposit_all_lines[-1].strip().split(',')
    name_end = line_end[0]
    date_end = line_end[1]
    money_end = line_end[2]
    loc_end = line_end[3]
    date_list = get_Every_Day(date_end, end_date)
    for i in range(0, len(date_list)):
    clean_out.write(name_end+','+date_list[i]+','+money_end+','+loc_end+' ')

    clean_out.close()


    add_data('111.csv', '2019/05/24')
  • 相关阅读:
    python学习笔记
    win10优化设置
    jpa基本用法
    5_方法(函数)、参数传递
    12_文件基本权限
    10_管理用户和组
    9_用户和组的相关配置文件
    7_vim 编辑器使用技巧
    8_Xmanager 远程连接 Linux 系统工具使用方法
    5_Linux系统目录结构,相对/绝对路径
  • 原文地址:https://www.cnblogs.com/zhanglin123/p/13253721.html
Copyright © 2020-2023  润新知