2014-03-18 127 views
0

我有一個來自加速度計(x,y,z)的數據字符串(在文本文件」XXX XXX XXX「中看起來像這樣,我試圖讀取它並轉換爲帶有三個子圖的數據線圖我正在修改一些朋友的代碼來做這件事,但我不確定其中一些錯誤是從哪裏來的,很明顯是初學者程序員,非常感謝。錯誤:與基地無效字面INT()10對於基數爲10的int()無效:「

import os 
import numpy as npy 
import matplotlib.pyplot as plt 
global y0,y1,y2 
increment_size = 8000 
datasample_size = 16000 

from os.path import join 
filepath = "C:\\Users\\Riley\\Documents\\Programming\\" 
infile = join(filepath, 'data.txt') 
infile = open(infile,"r") 
singleline = infile.readline() 
asciidata = singleline.split() 
asciidata[0]=asciidata[0][3:] 
y0=[int(asciidata[0])] 
y1=[int(asciidata[1])] 
y2=[int(asciidata[2])] 

count = 0 
for singleline in infile: 
    count += 1 
    if (count % 10000) == 0: 
     print(count) 
    asciidata = singleline.split() 
    y0.append(int(asciidata[0])) 
    y1.append(int(asciidata[1])) 
    y2.append(int(asciidata[2])) 
infile.close() 
totaldata=count-1 
print(totaldata) 

low = 0 
high = datasample_size 


while low < totaldata: 

    t = npy.arange(low,high) 

    plt.subplot(311) 
    plt.ylim(-2000,2000) 
    plt.plot(t,y0[low:high]) 

    plt.subplot(312) 
    plt.ylim(-2000,2000) 
    plt.plot(t,y1[low:high]) 

    plt.subplot(313) 
    plt.ylim(-2000,2000) 
    plt.plot(t,y2[low:high]) 

    outfilename = filepath + 'Plots/' + shortfilename + '_' + str(low) + '.png' 
    plt.savefig(outfilename) 

    outfilename2 = filepath + 'Datasegments/' + shortfilename + '_' + str(low) + '.txt' 
    outfile = open(outfilename2,"w") 
    for j in range(low,high): 
     outfile.write(str(y0[j])+'\t'+str(y1[j])+'\t'+str(y2[j])+'\n') 

# print(low), 

    plt.show() 


    low = low + increment_size 
    high = high + increment_size 
    if high > totaldata: 
     high = totaldata 

# if low > 10000: 
#  break 

# plt.close() 

回答

0

這可能是你可能試圖解析float()int()

如果您需要照顧的空值,嘗試int(s or 0)

0

有,做幾乎所有這一切爲您numpy的功能。這很難,我不知道你的數據文件的格式進行測試(這將有助於如果您在'data.txt'第幾行粘貼)

from os import path 

import numpy as npy 
import matplotlib.pyplot as plt 

increment_size = 8000 
datasample_size = 16000 

filepath = "C:\\Users\\Riley\\Documents\\Programming\\" 
infile = path.join(filepath, 'data.txt') 

# This line replaces all the file reading lines: 
y0, y1, y2 = npy.genfromtxt(infile, unpack=True) 

totaldata = len(y0) 
print(totaldata) 

low = 0 
high = datasample_size 

while low < totaldata: 
    ... 

可能繪製可以更簡單地太做了,但我米不知道我明白你爲什麼一節一節的繪圖。

相關問題