Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'lines.': 0.05; 'parameter': 0.05; 'python': 0.08; 'from:addr:python': 0.09; 'subject:file': 0.13; '"w")': 0.16; 'columns': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'guidance.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'timestamp': 0.16; 'wrote:': 0.16; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'code': 0.25; 'seconds': 0.25; 'all,': 0.27; 'import': 0.27; 'tried': 0.27; 'column': 0.28; 'pass': 0.28; 'example': 0.28; 'second': 0.29; 'lines': 0.30; 'splitting': 0.30; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.33; 'character': 0.34; 'received:84': 0.34; 'reply- to:addr:python.org': 0.34; 'try:': 0.34; 'file': 0.35; 'data.': 0.36; 'but': 0.37; 'another': 0.37; 'using': 0.37; 'some': 0.38; 'third': 0.38; 'skip:o 20': 0.38; 'characters': 0.38; 'processing': 0.39; 'subject:from': 0.39; 'except': 0.39; 'data,': 0.39; 'doing': 0.39; 'missing': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'data': 0.40; 'here': 0.64; 'become': 0.69; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'million': 0.76; '"3":': 0.84; '"5":': 0.84; 'luck.': 0.84; 'parts,': 0.84; 'subject:content': 0.84; 'fragment': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=A/buztqG c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=FFsdPcU1_MIA:10 a=A2dWweJ9qr0A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=UqNlubEn-IfR9Ko8tVEA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Sun, 22 Jan 2012 15:19:49 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Splitting a file from specific column content References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327245574 news.xs4all.nl 6972 [2001:888:2000:d::a6]:42180 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19215 On 22/01/2012 14:32, Yigit Turgut wrote: > Hi all, > > I have a text file approximately 20mb in size and contains about one > million lines. I was doing some processing on the data but then the > data rate increased and it takes very long time to process. I import > using numpy.loadtxt, here is a fragment of the data ; > > 0.000006 -0.0004 > 0.000071 0.0028 > 0.000079 0.0044 > 0.000086 0.0104 > . > . > . > > First column is the timestamp in seconds and second column is the > data. File contains 8seconds of measurement, and I would like to be > able to split the file into 3 parts seperated from specific time > locations. For example I want to divide the file into 3 parts, first > part containing 3 seconds of data, second containing 2 seconds of data > and third containing 3 seconds. Splitting based on file size doesn't > work that accurately for this specific data, some columns become > missing and etc. I need to split depending on the column content ; > > 1 - read file until first character of column1 is 3 (3 seconds) > 2 - save this region to another file > 3 - read the file where first characters of column1 are between 3 to > 5 (2 seconds) > 4 - save this region to another file > 5 - read the file where first characters of column1 are between 5 to > 5 (3 seconds) > 6 - save this region to another file > > I need to do this exactly because numpy.loadtxt or genfromtxt doesn't > get well with missing columns / rows. I even tried the invalidraise > parameter of genfromtxt but no luck. > > I am sure it's a few lines of code for experienced users and I would > appreciate some guidance. > Here's a solution in Python 3: input_path = "..." section_1_path = "..." section_2_path = "..." section_3_path = "..." with open(input_path) as input_file: try: line = next(input_file) # Copy section 1. with open(section_1_path, "w") as output_file: while line[0] < "3": output_file.write(line) line = next(input_file) # Copy section 2. with open(section_2_path, "w") as output_file: while line[5] < "5": output_file.write(line) line = next(input_file) # Copy section 3. with open(section_3_path, "w") as output_file: while True: output_file.write(line) line = next(input_file) except StopIteration: pass