0

I have a code whose outputs are mentioned below.

  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

I want to write these data to the .csv file for this: I make a array like: data=[['Total Bits',totalbits]]

and write this code to write data to the .csv file.

for row in data:
   for col in row:
    out.write('%d;'%col))

   out.write('\n')
  out.close()

But it gives me an error as first element in the column is a string, is there any way to write this data to the .csv file with or without converting into an array. The output in the .csv file looks like first column a description (string) and second the numbers (integers).

Total bits                       77826496
Total number of bits@0:          74653999
Total number of bits@1:          3172497
Total number of BRAM  bits@0:    17242039
Total number of BRAM  bits@1:    62089
Total number of non-BRAM  bits@0: 57411960
Total number of non-BRAM  bits@:  3110408
1
  • 1
    use the csv module. Commented May 22, 2017 at 23:35

2 Answers 2

2

You can use the format function. Like this:

data = [['Total Bits', 100]]
with open('output.csv','w') as out:
    for row in data:
        for col in row:
            out.write('{0};'.format(col))
        out.write('\n')
Sign up to request clarification or add additional context in comments.

Comments

1

You may try with csv module:

import csv
a = [['Total bits',77826496],['Total number of bits@0',74653999],['Total number of bits@1',3172497],\
     ['Total number of BRAM  bits@0',17242039],['Total number of BRAM  bits@1',62089],\
     ['Total number of non-BRAM  bits@0', 57411960],['Total number of non-BRAM  bits@',3110408]]
with open("output.csv", "wb") as f:
    writer = csv.writer(f,delimiter=':')
    writer.writerows(a)

output.csv file will be:

Total bits:77826496
Total number of bits@0:74653999
Total number of bits@1:3172497
Total number of BRAM  bits@0:17242039
Total number of BRAM  bits@1:62089
Total number of non-BRAM  bits@0:57411960
Total number of non-BRAM  bits@:3110408

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.