Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'log:': 0.05; 'float': 0.07; 'slow.': 0.09; 'subject:using': 0.09; 'python': 0.11; 'csv': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'once.': 0.16; 'received:192.168.1.4': 0.16; 'storing': 0.16; 'subject:Logging': 0.16; 'subject:PySerial': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'written': 0.21; 'header:User-Agent:1': 0.23; 'file.': 0.24; "i've": 0.25; 'script': 0.25; 'header:In-Reply- To:1': 0.27; "i'm": 0.30; 'quite': 0.32; 'subject:from': 0.34; 'could': 0.34; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'subject:data': 0.36; 'done': 0.36; 'doing': 0.36; 'skip:o 20': 0.38; 'filter': 0.38; 'to:addr:python-list': 0.38; 'moving': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'experts': 0.60; 'new': 0.61; "you're": 0.61; 'thomas': 0.65; 'here': 0.66; "'for'": 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=eZmzft0H c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=5FYZ9MsUIQAA:10 a=O_qnyQjlGLMA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=PV-7yLr6PzEA:10 a=ETQvedASZYBJXYooK58A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Tue, 04 Feb 2014 14:05:06 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Logging data from Arduino using PySerial References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391522708 news.xs4all.nl 2911 [2001:888:2000:d::a6]:60519 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65424 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: > [snip] > # 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) [snip] Try moving before the 'for' loop so it's done only once.