我正在尝试编写一个代码,从外部文件读取一组这样的值:
4.87320984E-09 1 49.882684417026184 0.18691906898647714 0.22222662961584136
1.88917193E-09 2 49.882684417026184 0.18691906898647714 0.22222662961584136
1.30942168E-09 3 49.882684417026184 0.18691906898647714 0.22222662961584136
7.26580629E-10 4 49.882684417026184 0.18691906898647714 0.22222662961584136
5.39025047E-10 5 49.882684417026184 0.18691906898647714 0.22222662961584136
2.04686401E-08 1 68.253977742324736 -5.0977927905402787E-002 0.32783928527485512
3.08666603E-09 3 68.253977742324736 -5.0977927905402787E-002 0.32783928527485512
2.88387625E-09 4 68.253977742324736 -5.0977927905402787E-002 0.32783928527485512
1.15430343E-09 5 68.253977742324736 -5.0977927905402787E-002 0.32783928527485512
我想将每列都存储在列表中,然后用它们做一些数学运算。
import sys
inFile = sys.argv[1]
c1 = []
c2 = []
c3 = []
c4 = []
c5 = []
data = [c1,c2,c3,c4,c5]
with open(inFile,'r') as i:
lines = i.read().split('\n')
for row in lines:
#split in columns and do some maths
print data
答案 0 :(得分:0)
with open("inFile",'r') as dataFile:
for i in dataFile.readlines():
print i.split()
<强>输出强>:
['4.87320984E-09', '1', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['1.88917193E-09', '2', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['1.30942168E-09', '3', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['7.26580629E-10', '4', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['5.39025047E-10', '5', '49.882684417026184', '0.18691906898647714', '0.22222662961584136']
['2.04686401E-08', '1', '68.253977742324736', '-5.0977927905402787E-002', '0.32783928527485512']
['3.08666603E-09', '3', '68.253977742324736', '-5.0977927905402787E-002', '0.32783928527485512']
['2.88387625E-09', '4', '68.253977742324736', '-5.0977927905402787E-002', '0.32783928527485512']
['1.15430343E-09', '5', '68.253977742324736', '-5.0977927905402787E-002', '0.32783928527485512']
答案 1 :(得分:0)
import pandas as pd
inFile = YOUR_CSV.csv
df = pd.read_csv(inFile)
print(df.head(3) #prints first 3 rows to check your data
答案 2 :(得分:0)
我肯定会推荐Pandas来完成这项任务。这样的事情应该有效
decLen = 3
decLen = 6
decLen = 0
delimiter参数指示它将一个或多个空白字符视为分隔符。对于示例中显示的数据,它应该可以工作。
答案 3 :(得分:0)
很多人推荐大熊猫,但由于这些数据只包含数字,你可能只想考虑numpy。这里有一个很好的教程:https://www.dataquest.io/blog/numpy-tutorial-python/。
以下是如何导入数据的示例。从那里你可以使用完整的numpy库来对你的数组进行数学运算或大量运算。有关详细信息,请参阅我发布的教程。
import numpy as np
file = 'path_to_your_file.csv'
data_array = np.genfromtxt(file, delimiter=',')
答案 4 :(得分:0)
以下是您要查找的代码:
cluster
但我建议使用Pandas read_csv方法。