python读取csv并转为数组,从CSV转换为Python中的数组

I have a CSV file containing the following.

0.000264,0.000352,0.000087,0.000549

0.00016,0.000223,0.000011,0.000142

0.008853,0.006519,0.002043,0.009819

0.002076,0.001686,0.000959,0.003107

0.000599,0.000133,0.000113,0.000466

0.002264,0.001927,0.00079,0.003815

0.002761,0.00288,0.001261,0.006851

0.000723,0.000617,0.000794,0.002189

I want convert the values into an array in Python and keep the same order (row and column). How I can achieve this?

I have tried different functions but ended with error.

解决方案

You should use the csv module:

import csv

results = []

with open("input.csv") as csvfile:

reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) # change contents to floats

for row in reader: # each row is a list

results.append(row)

This gives:

[[0.000264, 0.000352, 8.7e-05, 0.000549],

[0.00016, 0.000223, 1.1e-05, 0.000142],

[0.008853, 0.006519, 0.002043, 0.009819],

[0.002076, 0.001686, 0.000959, 0.003107],

[0.000599, 0.000133, 0.000113, 0.000466],

[0.002264, 0.001927, 0.00079, 0.003815],

[0.002761, 0.00288, 0.001261, 0.006851],

[0.000723, 0.000617, 0.000794, 0.002189]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值