写入csv文件,同时保持阵列的形状

时间:2016-07-15 23:08:07

标签: python csv numpy

我想写一个cvs文件,但它不再保留数组的形状,写一个等效的1d数组。

reader = open('/Users/williamneal/Scratch/Titanic/Employment.csv', 'rt')

csv_file_object = csv.reader(reader)
header = next(csv_file_object)

data=[]
for row in csv_file_object: 
    data.append(row)

data = np.array(data)

data = data.astype(float)

unemployment = data[:, 1:2]
total_employment = data[:, 2:3]
ratio = total_employment/unemployment

data = np.append(data, ratio, axis=1)

数据因此是一个形状为16,5的ndarray。我在最后的for循环中出错。

outputfile = open('/Users/williamneal/Scratch/Titanic/Employmentmodified.csv', 'wt')
write = csv.writer(outputfile, delimiter=' ')

for i in data:
        write.writerow(i)

任何建议,因为我无法从documentation开始。

Print screen of print

1 个答案:

答案 0 :(得分:0)

只需使用numpy:

In [10]: arr = np.random.random((16,5))

In [11]: arr
Out[11]: 
array([[ 0.11072668,  0.33415019,  0.799157  ,  0.19265392,  0.65449757],
       [ 0.06553107,  0.84417166,  0.04288959,  0.05092669,  0.6867636 ],
       [ 0.91626205,  0.85025192,  0.40024396,  0.56022321,  0.88459973],
       [ 0.46223145,  0.34691326,  0.05065761,  0.38820688,  0.46092017],
       [ 0.3275537 ,  0.10802874,  0.89195786,  0.85406217,  0.81956918],
       [ 0.78152943,  0.69585743,  0.0604771 ,  0.71730538,  0.06179689],
       [ 0.6502554 ,  0.39752148,  0.80819373,  0.5204556 ,  0.7796821 ],
       [ 0.39082875,  0.8306333 ,  0.96594533,  0.96884511,  0.65622016],
       [ 0.71411875,  0.56241831,  0.0986443 ,  0.02503774,  0.55637802],
       [ 0.71179835,  0.34359047,  0.65586152,  0.54577554,  0.19458417],
       [ 0.30939556,  0.89674838,  0.37678618,  0.36963822,  0.15355369],
       [ 0.31431116,  0.33293745,  0.36071818,  0.75510512,  0.53267805],
       [ 0.22621423,  0.12582219,  0.96648902,  0.09270175,  0.01831568],
       [ 0.47448967,  0.90287231,  0.52417831,  0.87637703,  0.17194786],
       [ 0.8583888 ,  0.27041963,  0.5764057 ,  0.78315845,  0.94293826],
       [ 0.40219331,  0.10778146,  0.92573301,  0.05465121,  0.52397036]])

In [12]: np.savetxt('random_array.csv', arr, fmt='%f', delimiter=',')

In [13]: cat random_array.csv
0.110727,0.334150,0.799157,0.192654,0.654498
0.065531,0.844172,0.042890,0.050927,0.686764
0.916262,0.850252,0.400244,0.560223,0.884600
0.462231,0.346913,0.050658,0.388207,0.460920
0.327554,0.108029,0.891958,0.854062,0.819569
0.781529,0.695857,0.060477,0.717305,0.061797
0.650255,0.397521,0.808194,0.520456,0.779682
0.390829,0.830633,0.965945,0.968845,0.656220
0.714119,0.562418,0.098644,0.025038,0.556378
0.711798,0.343590,0.655862,0.545776,0.194584
0.309396,0.896748,0.376786,0.369638,0.153554
0.314311,0.332937,0.360718,0.755105,0.532678
0.226214,0.125822,0.966489,0.092702,0.018316
0.474490,0.902872,0.524178,0.876377,0.171948
0.858389,0.270420,0.576406,0.783158,0.942938
0.402193,0.107781,0.925733,0.054651,0.523970

请注意,您可能需要使用fmt参数。 'f'表示类型的默认精度为6,但您可以使用增加到8,例如,使用'.8f'

In [18]: np.savetxt('random_array.csv', arr, fmt='%.8f', delimiter=',')

In [19]: cat random_array.csv
0.11072668,0.33415019,0.79915700,0.19265392,0.65449757
0.06553107,0.84417166,0.04288959,0.05092669,0.68676360
0.91626205,0.85025192,0.40024396,0.56022321,0.88459973
0.46223145,0.34691326,0.05065761,0.38820688,0.46092017
0.32755370,0.10802874,0.89195786,0.85406217,0.81956918
0.78152943,0.69585743,0.06047710,0.71730538,0.06179689
0.65025540,0.39752148,0.80819373,0.52045560,0.77968210
0.39082875,0.83063330,0.96594533,0.96884511,0.65622016
0.71411875,0.56241831,0.09864430,0.02503774,0.55637802
0.71179835,0.34359047,0.65586152,0.54577554,0.19458417
0.30939556,0.89674838,0.37678618,0.36963822,0.15355369
0.31431116,0.33293745,0.36071818,0.75510512,0.53267805
0.22621423,0.12582219,0.96648902,0.09270175,0.01831568
0.47448967,0.90287231,0.52417831,0.87637703,0.17194786
0.85838880,0.27041963,0.57640570,0.78315845,0.94293826
0.40219331,0.10778146,0.92573301,0.05465121,0.52397036