简体   繁体   English

将日期列添加到python中的附加输出csv文件

[英]adding date column to the appending output csv file in python

I use this code below to combine all csv files : below each file has 10,000 rows : 我在下面使用此代码合并所有csv文件:每个文件下面有10,000行:

billing_report_2014-02-01.csv billing_report_2014-02-02.csv : billing_report_2014-02-01.csv billing_report_2014-02-02.csv:

fout=open("out.csv","a")
for num in range(1,10):
    print num
    for line in open("billing_report_2014-02-0"+str(num)+".csv"):
         fout.write(line) 
for num in range(10,29):
    print num
    for line in open("billing_report_2014-02-"+str(num)+".csv"):
         fout.write(line) 
fout.close()

but now I want to add new date column to the out.csv file how can I add date column and have value of "2014-02-01" to every row that I append billing_report_2014-02-01 to out.csv, and value of "2014-02-02" to every row that I append billing_report_2014-02-02 to out.csv how can I approach this ? 但是现在我想向out.csv文件中添加新的日期列,如何添加日期列并将值“ 2014-02-01”添加到将billing_report_2014-02-01附加到out.csv的每一行中,并赋值我将billing_report_2014-02-02附加到out.csv的每一行中的“ 2014-02-02”的问题,我该如何处理?

List the filenames you want to work on, then take the data from that, build a generator over the input file that removes trailing new lines, and adds a new field with the date... eg: 列出您要使用的文件名,然后从中获取数据,在输入文件上构建一个生成器,以删除尾随的新行,并添加一个带有日期的新字段...例如:

filenames = [
  'billing_report_2014-02-01.csv',
  'billing_report_2014-02-02.csv'
]

with open('out.csv', 'w') as fout:
    for filename in filenames:
        to_append = filename.rpartition('_')[2].partition('.')[0]
        with open(filename) as fin:
            fout.writelines('{},{}\n'.format(line.rstrip(),to_append) for line in fin)

I think you can just add the date at the end: 我认为您可以在末尾添加日期:

for line in open("billing_report_2014-02-0"+str(num)+".csv"):
     fout.write(line+',DATE INFORMATION') 

I am presuming your CSV is really comma separated, if it is tab separted the characters should be \\t 我假设您的CSV确实是逗号分隔的,如果分开制表符,则字符应为\\ t

you could also use an intermediate step by changing line: 您还可以通过更改以下行来使用中间步骤:

line = line + ', DATE INFORMATION'

as you are trying to add the file name date just add it per variable: 当您尝试添加文件名日期时,只需将其添加到每个变量中即可:

line = line + ', 2014-02-0'+ str(num//10)

you could use the replace function if it is always the ",LLC" string expression, see the example below 您可以使用replace函数(如果它始终是“,LLC”字符串表达式),请参见下面的示例

>>> string = "100, 90101, California, Example company,LLC, other data"
>>> string.replace(',LLC',';LLC')
'100, 90101, California, Example company;LLC, other data'
>>> 

putting it all together and trying to bring some of the inspiration from @Jon CLements in as well (KUDOS!): 将所有内容放在一起,并尝试从@Jon CLements中引入一些灵感(KUDOS!):

def combine_and_add_date(year, month, startday, endday, replace_dict):
    fout=open("out.csv","a")
    for num in range(startday,endday+1):
        daynum = str(num)
        if len(daynum) ==1:
            daynum = '0'+daynum

        date_info = str(year+'-'month+'-'+daynum)
        source_name = 'billing_report_'+date_info+'.csv'

        for line in open(source_name):
            for key in replace_dict:
                line.replace(key,replact_dict[key])

            fout.write(line+','+date_info) 

    fout.close()

I hope this works and you should (hopefully I am a newb...) use it like this, note the dictionary is designed to allow you to make all kinds of replacements 我希望它能奏效,并且您应该(希望我是新手...)像这样使用它,请注意,该词典旨在允许您进行各种替换

combine_and_add_date("2014","02",1,28, {',LLC': ';LLC', ',PLC':';PLC'}) 

fingers crossed 手指交叉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM