将字符串和整数写入CSV文件 [英] Write String and integer to CSV file
问题描述
我有一个代码,其输出如下所述.
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
我想为此将这些数据写入.csv文件:我制作了一个数组,如:data=[['Total Bits',totalbits]]
I want to write these data to the .csv file for this: I make a array like: data=[['Total Bits',totalbits]]
并编写此代码以将数据写入.csv文件.
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()
但是它给我一个错误,因为列中的第一个元素是字符串,是否有任何方法可以将数据转换为.csv文件,无论是否转换为数组. .csv文件中的输出看起来像第一列是描述(字符串),其次是数字(整数).
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
推荐答案
您可以使用 format 函数.像这样:
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')
这篇关于将字符串和整数写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!