【发布时间】:2017-02-25 19:13:57
【问题描述】:
我的问题:我有一个 csv 文件,其中包含 90 到 3 米深的数据,并且这些数据会像这样来回传输。我正在使用最新的python。
例如。 (深度) 88, 77, 50, 20, 5, 90, 76, 54, 34, 15, 8, 4, 81, 74, 62,51, 49, 30, 22, 10, 8...等.它一直从 90 到 3,然后又回来。
我想要做的是每次数据介于 90 和 3 之间时将其分开。一旦分开,我想获取该列表中的最后一个值和第一个值。 像这样 前任。 88, 77, 50, 20, 5 (这里分开), 90, 76, 54, 34, 15, 8, 4 (这里分开) 81, 74, 62,51, 49, 30, 22, 10, 8 这里分开)... 等等。它一直从 90 到 3,然后又回来。
我该怎么做呢?或者我如何将它们分成列表,然后使用每个列表中的数据?
这是我目前的代码:
import csv, numpy
from collections import defaultdict
columns = defaultdict(list) # each value in each column is appended to a list
with open('C:\\Users\\AdamStoer\\Documents\\practicedata.csv') as f:
reader = csv.DictReader(f,delimiter=',') # read rows into a dictionary format
for row in reader:
r = float(row['roll'])
p = float(row['pitch'])
if 0.21 <= p <= 0.31:
if -0.06 <= r <= 0.06:
columns['pitch'].append(row['pitch'])
columns['roll'].append(row['roll'])
columns['i_depth'].append(row['i_depth'])
columns['irrad2'].append(row['sci_ocr504i_irrad2'])
print ('Pitch:')
print (columns['pitch'])
print ('Roll:')
print (columns['roll'])
print ('Depth')
print (columns['i_depth'])
print ("Irrandiance(2):")
print (columns['irrad2'])
irradlst = columns['irrad2']
irradfirst = irradlst[0]
irradlast = irradlst[-1]
depthlst = columns['i_depth']
depthfirst = depthlst[0]
depthlast = depthlst[-1]
print ("\nDepth 1 is " + depthfirst + " and " + "Depth 2 is " + depthlast)
print ("\nIrradiance 1 is " + irradfirst + " and " + "Irradiance 2 is " + irradlast)
#Find the Volume Attenuation Coefficient
#irranddiv = deepest/shallowest
irraddiv = float(irradfirst)/float(irradlast)
#depthdif = deepest-shallowest
depthdif = float(depthfirst) - float(depthlast)
#Find Log of irraddiv
irradlog = numpy.log(irraddiv)
#Find K
K = irradlog/(-depthdif)
print("\nAttenuation Coefficient")
print (K)
【问题讨论】:
标签: python python-3.x csv