尝试使用txt文件

时间:2018-12-30 18:41:40

标签: python

尝试使用txt文件从传感器提供的数据中绘制直方图。该文件具有连续运行的数据,应从第一行获取数据,但应在数据开头排除一些值并绘制图形,然后移至下一行以实时进行操作。尝试了几件事,但是没有用。我收到一个错误,无法将字符串转换为float:'27 / 12 / 2018-14:35:30'。

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib
    with open('load.txt', 'r') as fx:
        for line in fx:
            counter = True
            line = line.split(',')
            for thing in line:
                if counter:
                    counter = False
                else:
                    print(thing) 
    f= np.loadtxt('load.txt', delimiter=',', unpack= True)






    bins = [0,50,100,150,200]
    plt.hist(f, histtype = 'bar', bins = bins, rwidth=1, color='c')
    plt.xlabel('x values')
    plt.ylabel('y values')
    plt.title('OPC_N3 Histogram')
    #plt.legend()
    plt.show()

数据:但应排除初始值,例如在第一个数据块中,它应该从第一个0开始取值,以此类推,然后在下一个块中取值,并按直方图逐个输出每个数据块

2018年12月27日 14:35:30,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,29,27,114,1,8,95,231,137,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,6,0,4,60

27/12 / 2018-14:36:40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,78,12,114,1,72,97,93,128,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,6,0,74,100

27/12 / 2018-14:37:51,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,78,12,114,1,172,97,156,127,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,6,0,223,60

27/12 / 2018-14:39:02,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,78,12,114,1,1,98,185,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,6,0,130,60

27/12 / 2018-14:40:13,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,78,12,114,1,48,98,234,125,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,6,0,197,182

2 个答案:

答案 0 :(得分:0)

好吧,正如我所看到的,float的问题是您赋予了第一件事,就像我在代码中所做的那样,它应该被忽略,您可能想做的事情是对文件使用线程,但是在这种情况下,您将要从第一个文件中删除该行并将其添加到备份文件中,关于线程的最好的事情是它使您基本上可以不间断地运行它,甚至错误也无法停止该程序 这是一个与您现在Python multiprocess/multithreading to speed up file copying

类似的问题

P.S,如果您选择答案,请将问题标记为已回答,谢谢:)

答案 1 :(得分:0)

在拆分后添加此行

line=line.apply(lambda x: x[-1:])

祝你好运!