Home > other >  Python to TXT file import data are getting slower and slower, please?
Python to TXT file import data are getting slower and slower, please?

Time:11-30

Engineering dog, recently study and made a real-time access to information such as floating point Numbers and the output from the hardware to TXT, eventually converted to excel spreadsheet program, but even set during the operation of the sampling time interval (such as 1 second) 100 times, eventually exported data is never so much (up to 1 second 50 to 60 times), and with the increase of running time data is less and less, only 1 second about 10 times, would you please tell me what the good way to solve this situation? Thank you very much
 
The from __future__ import print_function
The from myo. Utils import TimeInterval
The import myo
The import sys
The import time
Import a datetime
The from txt_to_excel import *





Class Listener (myo DeviceListener) :

Def __init__ (self) :
The self. The interval=TimeInterval (None, 0.03) # acquisition time interval between the default 0.03
The self. "orientation=None
The self. The pose=myo. Pose. Rest
Self. Emg_enabled=True# defaults to False
Self. Locked=True# defaults to False lock vibration
The self. The rssi=None
The self. The emg=None

Def the output (self) :

If not the self. The interval. Check_and_reset () :
Return

With parts=[]
Parts. Append (' _ ') # empty out of the first column (because of the header problem of excel spreadsheet)
If the self. "orientation:
For comp in the self. "orientation:
Parts. Append (' {}} {: 4 f. The format (' 'if comp & gt;=0 else ', comp) # direction information
Parts. Append (STR (self. Pose). Ljust (24))
# parts. Append (' E 'if self. Emg_enabled else')
# parts. Append (' L '. If the self locked the else ')
Parts. Append (self. The rssi or 'NORSSI')
If self. Emg:
For comp in self. Emg:
Parts. Append (STR (comp). Ljust (3) # default of 5 emg between characters
Filename='C: \ \ Users \ likf \ Desktop \ \ test TXT' # import to specify TXT file
With the open (filename, 'a') as f_obj:
F_obj. Write (' \ r '+'. Join (' {}, '. The format for p (p) in parts) + '
+ datetime. Datetime. Now (). The strftime (' % % Y - m - H: % d % % m: % s. % f '))
# output file to TXT folder, behind the output and the current time and the timestamp is used to identify the

# the following a TXT file to excel
Filename="C: \ \ Users \ \ Desktop \ \ test \ l TXT" # need to translate TXT file address (can not to automatically generate)
Xlsname="C: \ \ Users \ \ Desktop \ \ test \ l XLS" # the preservation and XLS file name (can not to automatically generate)
Txt_xls (filename, xlsname)
Print (' \ r '+'. Join (' ({}). The format for p (p) in parts), end=' ')
Sys. Stdout. Flush ()




Def on_connected (self, event) :
Event. The device. The request_rssi ()

Def on_rssi (self, event) :
The self. The rssi=event. Rssi
The self. The output ()

Def on_pose (self, event) :
"' on_pose double-click gesture is detected after open the EMG signal (off by default)
Modified to input the first random gestures after open "'
The self. The pose=event. Pose
# if self. Pose==myo. Pose. Double_tap: modified
Event. The device. The stream_emg (True)
Self. Emg_enabled=True
# elif self. Pose==myo. Pose. Fingers_spread: modified
# if self. Pose==myo. Pose. Fingers_spread:
# event. Device. Stream_emg (False)
# self. Emg_enabled=False
# self. Emg=None
The self. The output ()

Def on_orientation (self, event) :
The self. "orientation=event. Orientation
The self. The output ()

Def on_emg (self, event) :
The self. The emg=event. Emg
The self. The output ()

Def on_unlocked (self, event) :
Self. Locked=False
The self. The output ()

Def on_locked (self, event) :
Self. Locked=True
The self. The output ()


If __name__=="__main__ ':
Myo. Init (bin_path='../MYO_SDK/bin ') # special note: the need to enter the current computer/MYO_SDK/bin file directory address
The hub=myo. Hub ()
The listener=listener ()


While the hub. The run (listener. On_event, 500) : # 500 said 500 ms acquisition time, feeling and display only, nothing to do with actual collection rate
Print (time. Strftime (" % Y - % m M_ H_ - % d % % % S ", the time the localtime ())) # here added timestamp (pycharam display)
Pass
  • Related