0

I have 50 files that I process, then write output of the each file to the .csv file, but, it gives me only the last file output to the .csv file

with open('output.csv','a+') as out:
    out.write(filename)
    out.write('\n')
    out.write('\n') 
    out.write(';''Golden readback from vivado before fault-injection')
    out.write(';''Readback from vivado after the injection')
    out.write(';''Real-Delta')
    out.write('\n')
# original is my data.
    for row in original:
     for col in row:      
      out.write('{0};'.format(col))
     out.write('\n')
    out.write('\n')
out.close()  

Any idea how can I put all files output to the same .csv file.

5
  • 1
    try with only "a" instead a+ and do not out.close() Commented May 27, 2017 at 15:59
  • 1
    Use the csv module to create a csv file. Also, out.close() isn't necessary; the with statement will call it implicitly. Commented May 27, 2017 at 16:01
  • I have already tried with it but no luck. Commented May 27, 2017 at 16:07
  • What is csv module to create the .csv file, would you please explain @chepner Commented May 27, 2017 at 16:08
  • It's a module in the standard library. Commented May 27, 2017 at 16:21

2 Answers 2

1

You are appending your results in your code using 'a+' mode, try 'w+' instead, it should work.

with open('output.csv','w+') as out:
    out.write(filename)
    out.write('\n')
    out.write('\n') 
    out.write(';''Golden readback from vivado before fault-injection')
    out.write(';''Readback from vivado after the injection')
    out.write(';''Real-Delta')
    out.write('\n')
# original is my data.
    for row in original:
     for col in row:      
      out.write('{0};'.format(col))
     out.write('\n')
    out.write('\n')
out.close()
Sign up to request clarification or add additional context in comments.

4 Comments

@hassan Does original have all the data or is this the code you run for every file after you process it?
The original has the data that computed for each run. It run for every file @Arnab Sharma
try using file = open('output.csv', 'a+') for row in original: for col in row: file.write(str(col))
I already tried. No idea what's the problem. May be every time input file changes ?
0
import glob
from itertools import islice
import linecache

path = '*.rbd'
#path='original-adder.rbd'
files=sorted(glob.glob(path))

for filename in files:

 del_lines = 101
 with open(filename,'r') as f:

  i=1
  while i <= del_lines:
   line1 = f.readline()
   i+=1

  i=0    
  count0_bram = 0
  count1_bram = 0
  count0_nonbram = 0
  count1_nonbram = 0
  totalbits = 0
  totalbitsones = 0
  totalbitszeros = 0

  NON_BRAM_bits_zero_original=57411950
  NON_BRAM_bits_ones_original=3110418

  difference_zero_non_BRAM = 0
  difference_ones_non_BRAM = 0


# COUNT NON BRAM

  for i in range(102,1891426):
   line=linecache.getline(filename,i)

   count_zeros=line.count('0')
   count0_nonbram=count0_nonbram+count_zeros

   count_ones=line.count('1')
   count1_nonbram=count1_nonbram+count_ones

  i=0
# to count BRAM

 #lines=islice(fin,1891427,1903714)
  for i in xrange(1891427,2432181):
    line=linecache.getline(filename,i)


   #line=line.strip()
    count_zeros=line.count('0')
    count0_bram=count0_bram+count_zeros

    count_ones=line.count('1')
    count1_bram=count1_bram+count_ones
  i=0

  totalbits=count0_bram+count1_bram+count0_nonbram+count1_nonbram
  totalbitsones=count1_bram+count1_nonbram
  totalbitszeros=count0_bram+count0_nonbram
  difference_zero_non_BRAM = count0_nonbram-   NON_BRAM_bits_zero_original # new -old
  difference_ones_non_BRAM = count1_nonbram-NON_BRAM_bits_ones_original # new - old


  print filename
  print "-------------------------------------------------"
  print "Total Bits:%d"%totalbits
  print "Number of totalbits-zeros: %d." %totalbitszeros
  print "Number of totalbits-ones: %d." %totalbitsones
  print "Number of BRAM-Zeros: %d." %count0_bram
  print "Number of BRAM-ones: %d." %count1_bram
  print "Number of NON_BRAM-Zeros: %d." %count0_nonbram
  print "Number of NON_BRAM-Ones: %d." %count1_nonbram
  print "difference_zero_non_BRAM:%d."%difference_zero_non_BRAM
  print "difference_ones_non_BRAM:%d."%difference_ones_non_BRAM
  print "--------------------------------------------------"



original= [['Total Bits', 77826496,totalbits,totalbits-77826496],['Total number of bits @0',74651972,totalbitszeros,totalbitszeros-74651972],['Total number of bits @1',3174524,totalbitsones,totalbitsones-3174524],['Totalnumber of BRAM bits-zero',17240022,count0_bram,count0_bram-17240022],['Total number of BRAM bits-ones',64106,count1_bram,count1_bram-64106],['Total  number of non-BRAM bits@0',57411950,count0_nonbram,count0_nonbram-57411950],['Total number of non-BRAM bits@1',3110418,count1_nonbram,count1_nonbram-3110418]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.