0

I would like to create a file in real time and add the values corresponding to the columns to an existing file in real time in the corresponding CSV file.

How can I add each of the CSV files that I generate in that program?

I'll write down the code I'm using now.

import csv

for i in range(10):
SD="Save datas(Angle)"+str(i)  ## 해당 각도별로 배열을 지정

SDArray1=str(SD)               ## 파일을 만들어준다

f=open(SDArray1+".csv","a+t")#  ## 이름을 만들어준 파일을 생성

csv_writer = csv.writer(f)
csv_writer.writerow([SD])
print("One loop has started")
f.close()#

for i in range(1,5):
    cdata=[i]
    f=open(SDArray1+".csv","a+t")

    csv_writer =csv.writer(f)
    csv_writer.writerow(cdata)

    print(cdata)
    f.close()#
    print("loop's finished!")

If you look at the code above, a certain file is created. I completed the next file, but I was wondering how to add columns to the file.

David
  • 1,192
  • 5
  • 13
  • 30
임종훈
  • 147
  • 1
  • 3
  • 8
  • Why is it necessary to keep opening and closing the file within the loop? – strava Sep 01 '18 at 07:23
  • 1
    Possible duplicate of [How to add a new column to a CSV file?](https://stackoverflow.com/questions/11070527/how-to-add-a-new-column-to-a-csv-file) – strava Sep 01 '18 at 07:25

1 Answers1

1

csv.write_row() takes a complete row of columns - if you need more, add them to your cdata=[i]- f.e. cdata=[i,i*2,i*3,i*4].

You should use with open() as f: for file manipulation, it is more resilient against errors and autocloses the file when leaving the with-block.

Fixed:

import csv

# do not use i here and down below, thats confusing, better names are a plus
for fileCount in range(10):
    filename = "filename{}.csv".format(fileCount) # creates filename0.csv ... filename9.csv 

    with open(filename,"w") as f:#  # create file new
        csv_writer = csv.writer(f)
        # write headers
        csv_writer.writerow(["data1","data2","data3"])
        # write 4 rows of data
        for i in range(1,5):
            cdata=[(fileCount*100000+i*1000+k) for k in range(3)] # create 3 datapoints
            # write one row of data [1000,1001,1002] up to [9004000,9004001,9004002] 
            # for last i and fileCount
            csv_writer.writerow(cdata)

# no file.close- leaving wiht open() scope autocloses

Check what we have written:

import os
for d in sorted(os.listdir("./")):
    if d.endswith("csv"): 
        print(d,":") 
        print("*"*(len(d)+2)) 
        with open(d,"r") as f: 
            print(f.read()) 
        print("")

Output:

filename0.csv :
***************
data1,data2,data3
1000,1001,1002
2000,2001,2002
3000,3001,3002
4000,4001,4002


filename1.csv :
***************
data1,data2,data3
101000,101001,101002
102000,102001,102002
103000,103001,103002
104000,104001,104002

filename2.csv :
***************
data1,data2,data3
201000,201001,201002
[...snip the rest - you get the idea ...]     

filename9.csv :
***************
data1,data2,data3
901000,901001,901002
902000,902001,902002
903000,903001,903002
904000,904001,904002

To add a new column to an existing file:

  • open old file to read
  • open new file to write
  • read the old files header, add new column header and write it in new file
  • read all rows, add new columns value to each row and write it in new file

Example:

Adding the sum of column values to the file and writing as new file:

filename = "filename0.csv"
newfile =  "filename0new.csv"

# open one file to read, open other (new one) to write
with open(filename,"r") as r, open(newfile,"w") as w:
    reader = csv.reader(r)
    writer = csv.writer(w)

    newHeader = next(reader)   # read the header
    newHeader.append("Sum")    # append new column-header
    writer.writerow(newHeader) # write header

    # for each row: 
    for row in reader:
        row.append(sum(map(int,row)))   # read it, sum the converted int values
        writer.writerow(row)            # write it

# output the newly created file:
with open(newfile,"r") as n:
    print(n.read())

Output:

data1,data2,data3,Sum
1000,1001,1002,3003
2000,2001,2002,6003
3000,3001,3002,9003
4000,4001,4002,12003
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thank you for your opinion.But what I want to know is how do I add Columns value to a newly created file when there is an existing file and a new one is created? – 임종훈 Sep 01 '18 at 08:18
  • This is how it works. But what I really want to do is measure data in real time and save it as a csv file and merge it into one and draw a graph. What should I do? – 임종훈 Sep 02 '18 at 09:52
  • @임종훈 Writing, reading, adding to it, ... with files makes this IO-bound. Not a good idea, as IO is slow. Why not simply do it in memory and write your CSV after you took all measurements? how many datapoints are we talking about? – Patrick Artner Sep 02 '18 at 09:55
  • I want to thank you for that. As I thought about the data size growing in the current job, I selected to create a file and combine the csv file using the pandas. Thank you for your time. – 임종훈 Sep 03 '18 at 16:23