简体   繁体   中英

appending dates by columns in a csv file in python

I have a script who goes through all the files within a given path('C:\\Users\\Razvi\\Desktop\\italia') and reads the number of lines from each file found with the name "cnt.csv" and then it writes in another file named "counters.csv" the current date + the name of folder + the sum of lines found in the "cnt.csv".

Now the output file("counters.csv") looks like this:

30/9/2017   
8dg5    5
8dg7    7

01/10/2017  
8dg5    8
8dg7    10

In which the names 8dg5 and 8dg7 are the folders where the script found the file"cnt.csv" aand the numers 5,7,8,10 are the sum of lines found in csv files every time when i run the script and obviously the date when i run the script it's appended every time.

Now what i want it's to write those dates in csv, somehow appended on columns , not lines, something like this:

30/9/2017   01/10/2017
8dg5    5   8dg5    8
8dg7    7   8dg7    10

Here is the code:

import re
import os
from datetime import datetime

#italia =r'C:\Users\timraion\Desktop\italia'
italia =r'C:\Users\Razvi\Desktop\italia'
file = open(r'C:\Users\Razvi\Desktop\counters.csv', 'a')
now=datetime.now()
dd=str(now.day)
mm=str(now.month)
yyyy=str(now.year)
date=(dd + "/" + mm + "/" + yyyy)
file.write(date + '\n')
for filename in os.listdir(italia):
    infilename = os.path.join(italia, filename)
    for files in os.listdir(infilename):
        if files.endswith("cnt.csv"):
            result=os.path.join(infilename,"cnt.csv")
            print(result)
            infilename2 = os.path.join(infilename, files)
            lines = 0
            for line in open(infilename2):
                    lines += 1



            file = open(r'C:\Users\Razvi\Desktop\counters.csv', 'a')
            file.write(str(filename) +","+ str(lines)+"\n" )
            file.close()

Thanks!

If you want to add the new entries as columns instead of rows further down, you'll have to read in the counters.csv file each time, append your columns, and rewrite it.

import os
from datetime import datetime
import csv

italia =r'C:\Users\Razvi\Desktop\italia'
counts = []
for filename in os.listdir(italia):
    infilename = os.path.join(italia, filename)
    for files in os.listdir(infilename):
        if files.endswith("cnt.csv"):
            result=os.path.join(infilename,"cnt.csv")
            print(result)
            infilename2 = os.path.join(infilename, files)
            lines = 0
            for line in open(infilename2):
                lines += 1
            counts.append((filename, lines))  # save the filename and count for later
if os.path.exists(r'C:\Users\Razvi\Desktop\counters.csv'):
    with open(r'C:\Users\Razvi\Desktop\counters.csv', 'r') as csvfile:
        counters = [row for row in csv.reader(csvfile)]    # read the csv file
else:
    counters = [[]]
now=datetime.now()
# Add the date and a blank cell to the first row
counters[0].append('{}/{}/{}'.format(now.day, now.month, now.year))
counters[0].append('')
for i, count in enumerate(counts):
    if i + 1 == len(counters):    # check if we need to add a row
        counters.append(['']*(len(counters[0])-2))
    while len(counters[i+1]) < len(counters[0]) - 2:
        counters[i+1].append('')  # ensure there are enough columns in the row we're on
    counters[i+1].extend(count)    # Add the count info as two new cells
with open(r'C:\Users\Razvi\Desktop\counters.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)    # write the new csv file
    for row in counters:
        writer.writerow(row)

Another note, file is a builtin function in Python, so you shouldn't use that name as a variable, since unexpected stuff might happen further down in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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