我正在尝试为我的公司编写一个简单的绘图程序。我有一个带数据的.dat文件,我可以读取它:
with open(r'XXX\DAT-010.DAT', 'r') as f:
data = f.readlines()
print(data)
结果:
[' Date Time Elapsed Sensor1 Sensor2 Sensor3 Sensor4 Sensor5 Sensor6 Sensor7 Sensor8 Sensor9 Sensor10 Sensor11 Sensor12 Sensor13 Sensor14 Sensor15 Sensor16 Sensor17 Sensor18 Sensor19 Sensor20\n',
'dd/mm/yyyy hh:mm:ss Seconds JGP1103-I2 JGP1102-I2 JGP1102-I1 JGP1001 JGP1101 FLOW_416 FLOW_333 FLOW_2945 L1_INJ L2_INJ L3_INJ L4_INJ L1_EXT L2_EXT L3_EXT L4_EXT L1_Mth_ext L2_Mth_ext L3_Mth_ext L4_Mth_ext\n',
' - - - kPa kPa kPa kPa kPa ml/min ml/min ml/min mV mV mV mV mV mV mV mV mV mV mV mV\n',
' - - - - - - - - - - - - - - - - - - - - - - -\n',
'---------- -------- --------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------\n',
'26.10.2016 08:58:09 1211242 84.95 84.77 86.21 84.47 84.77 -104.78 -83.82 - - - - - - - - - - - - -\n',
'26.10.2016 08:58:24 1211257 85.01 84.77 86.03 84.53 84.77 -104.78 -83.82 - - - - - - - - - - - - -\n']
现在,要查找实际值,我正在做
data_int = list(map(float, data))
,我收到以下错误消息:
ValueError: could not convert string to float: ' Date Time Elapsed Sensor1 Sensor2 Sensor3 Sensor4 Sensor5 Sensor6 Sensor7 Sensor8 Sensor9 Sensor10 Sensor11 Sensor12 Sensor13 Sensor14 Sensor15 Sensor16 Sensor17 Sensor18 Sensor19 Sensor20\n'
我做到了:
data_int = list(map(float, data[6]))
在仅应包含实际数据值的行上进行尝试,我得到了:
ValueError: could not convert string to float: '.'
现在,如何才能有效地将此数据转换为可分析的值列表?如何将txt数据转换为整数?为了进行记录,我尝试了int(data)等,但没有用。
答案 0 :(得分:1)
文件的每一行都是一个字符串-因此Available checkers: jslint
包含:
row[5]
您不能将其转换为浮点数。您需要
"26.10.2016 08:58:09 1211242 84.95 84.77 86.21 84.47 84.77 -104.78 -83.82 - - - - - - - - - - - - -\n"
输出(值列表)
line = "26.10.2016 08:58:09 1211242 84.95 84.77 86.21 84.47 84.77 -104.78 -83.82 - - - - - - - - - - - - -\n"
def tryFloat(text):
"""Returns either the float(text) or text itself."""
try:
return float(text)
except:
return text
# strip() removes \n and other witespaces front and end
# split splits at whitespaces combining multiple into one
data = list(map(tryFloat,line.strip().split()))
print(data)
然后您可以从该列表中选择零件:
['26.10.2016', '08:58:09', 1211242.0, 84.95, 84.77, 86.21, 84.47, 84.77, -104.78,
-83.82, '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']