记录一下读取txt文件数据,其实不只是txt文件适用,可以使用文本打开的文档数据应该都差不多,我这次读取的数据是nmd后缀格式的文件,但是用记事本可以打开。就是用读txt的办法试试,结果成功了。
以前也给朋友写过txt数据读取方法帮他做博士论文处理数据,想着比较简单就不记录了,结果时间长了就忘了,需要的时候又得重新写。所以就简单做个笔记以便后面需要直接抄写。
import pandas as pd
import numpy as np
import pyecharts.options as opts
from pyecharts.charts import Line
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def main():
datas = []
buff = 0
timeNum = 0
time = 0
frequency = 0.000125
with open("123.nmd") as f:
for line in f.readlines():
line = line.strip('\n').split()
if timeNum == 3:
time = int(line[0])
timeNum = timeNum + 1
if is_number(line[0]) and len(line) > 2 :
data = []
data.append(round((float(line[0]) * frequency),8)) #时间
data.append(float(line[2])) #
data.append(float(line[3]))
if int(line[0]) == 0:
data.append(0)
else:
data.append((float(line[2]) - buff) * (-1))
datas.append(data)
buff = float(line[2])
npDatas = np.array(datas)
print(npDatas)
with open("test.txt","w") as f:
for dLines in range(len(datas)):
for dLine in range(len(datas[dLines])):
f.write(str(datas[dLines][dLine]) + '\t')
f.write('\n')
if '__main__' == __name__:
main()
数据demo
[NuMotion System]
the system used to do datalog
[Sample Freq]
8000
[Data Meaning]
[Data Infomation]
Samples Time_Stamp SP_TABLE_ENC_PORT_0 SP_TABLE_CMDPOS_PORT
0 0x63E4C969 2146840446.000000 2146840071.000000
1 0x63E4D8A9 2146840373.000000 2146839998.000000
2 0x63E4E7E9 2146840300.000000 2146839924.000000
3 0x63E4F729 2146840227.000000 2146839851.000000
4 0x63E50669 2146840152.000000 2146839778.000000
5 0x63E515A9 2146840080.000000 2146839704.000000
6 0x63E524E9 2146840006.000000 2146839631.000000
7 0x63E53429 2146839933.000000 2146839558.000000
8 0x63E54369 2146839859.000000 2146839484.000000
9 0x63E552A9 2146839787.000000 2146839411.000000
10 0x63E561E9 2146839713.000000 2146839337.000000
11 0x63E57129 2146839638.000000 2146839264.000000
12 0x63E58069 2146839566.000000 2146839191.000000
13 0x63E58FA9 2146839492.000000 2146839117.000000
14 0x63E59EE9 2146839419.000000 2146839044.000000
15 0x63E5AE29 2146839345.000000 2146838971.000000
16 0x63E5BD69 2146839273.000000 2146838897.000000
17 0x63E5CCA9 2146839200.000000 2146838824.000000
18 0x63E5DBE9 2146839124.000000 2146838750.000000
19 0x63E5EB29 2146839052.000000 2146838677.000000
20 0x63E5FA69 2146838978.000000 2146838603.000000
21 0x63E609A9 2146838906.000000 2146838530.000000
22 0x63E618E9 2146838831.000000 2146838457.000000
23 0x63E62829 2146838759.000000 2146838383.000000
24 0x63E63769 2146838685.000000 2146838310.000000
25 0x63E646A9 2146838610.000000 2146838236.000000
26 0x63E655E9 2146838538.000000 2146838163.000000
27 0x63E66529 2146838464.000000 2146838089.000000
28 0x63E67469 2146838391.000000 2146838016.000000
29 0x63E683A9 2146838317.000000 2146837942.000000
30 0x63E692E9 2146838244.000000 2146837869.000000
31 0x63E6A229 2146838171.000000 2146837795.000000
32 0x63E6B169 2146838096.000000 2146837722.000000
33 0x63E6C0A9 2146838023.000000 2146837648.000000
34 0x63E6CFE9 2146837949.000000 2146837575.000000
35 0x63E6DF29 2146837876.000000 2146837502.000000
36 0x63E6EE69 2146837802.000000 2146837428.000000
37 0x63E6FDA9 2146837730.000000 2146837355.000000
38 0x63E70CE9 2146837656.000000 2146837281.000000
39 0x63E71C29 2146837581.000000 2146837208.000000
40 0x63E72B69 2146837509.000000 2146837134.000000
41 0x63E73AA9 2146837435.000000 2146837060.000000
42 0x63E749E9 2146837361.000000 2146836987.000000
43 0x63E75929 2146837288.000000 2146836913.000000
44 0x63E76869 2146837214.000000 2146836840.000000
45 0x63E777A9 2146837141.000000 2146836766.000000
46 0x63E786E9 2146837066.000000 2146836693.000000
47 0x63E79629 2146836994.000000 2146836619.000000
48 0x63E7A569 2146836920.000000 2146836546.000000
49 0x63E7B4A9 2146836847.000000 2146836472.000000
50 0x63E7C3E9 2146836772.000000 2146836399.000000
51 0x63E7D329 2146836699.000000 2146836325.000000
52 0x63E7E269 2146836625.000000 2146836252.000000
53 0x63E7F1A9 2146836551.000000 2146836178.000000
54 0x63E800E9 2146836479.000000 2146836104.000000
55 0x63E81029 2146836405.000000 2146836031.000000
56 0x63E81F69 2146836331.000000 2146835957.000000
57 0x63E82EA9 2146836257.000000 2146835884.000000
58 0x63E83DE9 2146836184.000000 2146835810.000000
59 0x63E84D29 2146836110.000000 2146835736.000000
60 0x63E85C69 2146836036.000000 2146835663.000000
61 0x63E86BA9 2146835963.000000 2146835589.000000
62 0x63E87AE9 2146835890.000000 2146835516.000000
63 0x63E88A29 2146835816.000000 2146835442.000000
64 0x63E89969 2146835742.000000 2146835368.000000
65 0x63E8A8A9 2146835669.000000 2146835295.000000
66 0x63E8B7E9 2146835594.000000 2146835221.000000
67 0x63E8C729 2146835521.000000 2146835148.000000
68 0x63E8D669 2146835448.000000 2146835074.000000
69 0x63E8E5A9 2146835374.000000 2146835000.000000
70 0x63E8F4E9 2146835300.000000 2146834927.000000
71 0x63E90429 2146835226.000000 2146834853.000000
72 0x63E91369 2146835152.000000 2146834779.000000
73 0x63E922A9 2146835080.000000 2146834706.000000
74 0x63E931E9 2146835004.000000 2146834632.000000
75 0x63E94129 2146834932.000000 2146834558.000000
76 0x63E95069 2146834858.000000 2146834485.000000
处理完的结果
0.0 2146840446.0 2146840071.0 0
0.000125 2146840373.0 2146839998.0 73.0
0.00025 2146840300.0 2146839924.0 73.0
0.000375 2146840227.0 2146839851.0 73.0
0.0005 2146840152.0 2146839778.0 75.0
0.000625 2146840080.0 2146839704.0 72.0
0.00075 2146840006.0 2146839631.0 74.0
0.000875 2146839933.0 2146839558.0 73.0
0.001 2146839859.0 2146839484.0 74.0
0.001125 2146839787.0 2146839411.0 72.0
0.00125 2146839713.0 2146839337.0 74.0
0.001375 2146839638.0 2146839264.0 75.0
0.0015 2146839566.0 2146839191.0 72.0
0.001625 2146839492.0 2146839117.0 74.0
0.00175 2146839419.0 2146839044.0 73.0
0.001875 2146839345.0 2146838971.0 74.0
0.002 2146839273.0 2146838897.0 72.0
0.002125 2146839200.0 2146838824.0 73.0
0.00225 2146839124.0 2146838750.0 76.0
0.002375 2146839052.0 2146838677.0 72.0
0.0025 2146838978.0 2146838603.0 74.0
0.002625 2146838906.0 2146838530.0 72.0
0.00275 2146838831.0 2146838457.0 75.0
0.002875 2146838759.0 2146838383.0 72.0
0.003 2146838685.0 2146838310.0 74.0
0.003125 2146838610.0 2146838236.0 75.0
0.00325 2146838538.0 2146838163.0 72.0
0.003375 2146838464.0 2146838089.0 74.0
0.0035 2146838391.0 2146838016.0 73.0
0.003625 2146838317.0 2146837942.0 74.0
0.00375 2146838244.0 2146837869.0 73.0
0.003875 2146838171.0 2146837795.0 73.0
0.004 2146838096.0 2146837722.0 75.0
0.004125 2146838023.0 2146837648.0 73.0
0.00425 2146837949.0 2146837575.0 74.0
0.004375 2146837876.0 2146837502.0 73.0
0.0045 2146837802.0 2146837428.0 74.0
0.004625 2146837730.0 2146837355.0 72.0
0.00475 2146837656.0 2146837281.0 74.0
0.004875 2146837581.0 2146837208.0 75.0
0.005 2146837509.0 2146837134.0 72.0
0.005125 2146837435.0 2146837060.0 74.0
0.00525 2146837361.0 2146836987.0 74.0
0.005375 2146837288.0 2146836913.0 73.0
0.0055 2146837214.0 2146836840.0 74.0
0.005625 2146837141.0 2146836766.0 73.0
0.00575 2146837066.0 2146836693.0 75.0
0.005875 2146836994.0 2146836619.0 72.0
0.006 2146836920.0 2146836546.0 74.0
0.006125 2146836847.0 2146836472.0 73.0
0.00625 2146836772.0 2146836399.0 75.0
0.006375 2146836699.0 2146836325.0 73.0
0.0065 2146836625.0 2146836252.0 74.0
0.006625 2146836551.0 2146836178.0 74.0
0.00675 2146836479.0 2146836104.0 72.0
0.006875 2146836405.0 2146836031.0 74.0
0.007 2146836331.0 2146835957.0 74.0
0.007125 2146836257.0 2146835884.0 74.0
0.00725 2146836184.0 2146835810.0 73.0
0.007375 2146836110.0 2146835736.0 74.0
0.0075 2146836036.0 2146835663.0 74.0
0.007625 2146835963.0 2146835589.0 73.0
0.00775 2146835890.0 2146835516.0 73.0
0.007875 2146835816.0 2146835442.0 74.0
0.008 2146835742.0 2146835368.0 74.0
0.008125 2146835669.0 2146835295.0 73.0
0.00825 2146835594.0 2146835221.0 75.0
0.008375 2146835521.0 2146835148.0 73.0
0.0085 2146835448.0 2146835074.0 73.0
0.008625 2146835374.0 2146835000.0 74.0
0.00875 2146835300.0 2146834927.0 74.0
0.008875 2146835226.0 2146834853.0 74.0
0.009 2146835152.0 2146834779.0 74.0
0.009125 2146835080.0 2146834706.0 72.0
0.00925 2146835004.0 2146834632.0 76.0
0.009375 2146834932.0 2146834558.0 72.0