我有来自perfmon的.csv文件。该文件有6000条记录,如下所示:
(PDH-CSV 4.0) (SA Pacific Standard Time)(300),"\\server1\PhysicalDisk(_Total)\% Disk Read Time","\\server1\PhysicalDisk(_Total)\% Disk Write Time"
10/30/2017 15:00:15.568," "," "
10/30/2017 15:00:30.530,"25.763655942362824","130.21748494987176"
10/30/2017 15:00:45.518,"25.591636684958058","135.81093813384427"
我需要从1列和2列获得最小值,最大值和95%。但是,作为一个新手,我无法通过第一个挑战,即将每个值格式化为int:
import csv
sum = 0
fila = 0
with open('datos_header.csv') as csvfile:
leercsv = csv.reader(csvfile, delimiter = ',')
csvfile.__next__()
for col in leercsv:
col1 = (col[1])
subtot = float(col1 * 4)
# fila = fila + 1
# sum = col1 + float(col)
#tot = sum / fila
print(subtot)
并获得:
追踪(最近一次通话): 文件“”,第10行,in ValueError:无法将字符串转换为float:
我试过了: - 删除标题 - 使用正则表达式删除每个非数字的类似/或:值 - 删除空白空白
说完了:
谢谢!
答案 0 :(得分:1)
你必须先检查字符串浮动转换,所以你可以尝试:
for col in leercsv:
col1 = (col[1])
if col1: subtot = float(col1) * 4 # and convert to float before multiply
更强大的解决方案:
for col in leercsv:
col1 = (col[1])
try: subtot = float(col1) * 4
except: pass