Logging data from Arduino using PySerial

MRAB python at mrabarnett.plus.com
Tue Feb 4 15:05:06 CET 2014

On 2014-02-04 04:07, Thomas wrote:
> I've written a script to log data from my Arduino to a csv file. The script works well enough but it's very, very slow. I'm quite new to Python and I just wanted to put this out there to see if any Python experts could help optimise my code. Here it is:
>      # Cleaning the data_log and storing it in data.csv
>      with open('data.csv','wb') as csvfile:
>          for line in data_log:
>              line_data = re.findall('\d*\.\d*',line) # Find all digits
>              line_data = filter(None,line_data)    # Filter out empty strings
>              line_data = [float(x) for x in line_data] # Convert Strings to float
>              for i in range(1,len(line_data)):
>                  line_data[i]=map(line_data[i],0,1023,0,5)
You're doing this for every in line the log:

>              csvwrite = csv.writer(csvfile)

Try moving before the 'for' loop so it's done only once.

More information about the Python-list mailing list