Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!nuzba.szn.dk!pnx.dk!amsnews11.chello.com!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.036 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'skip': 0.04; 'tab': 0.07; 'python': 0.08; 'expressions.': 0.09; 'subject:Processing': 0.09; '-----------': 0.15; 'case.': 0.15; 'confused.': 0.16; 'header:X-Face:1': 0.16; 'received:lan': 0.16; 'workaround': 0.16; 'looked': 0.16; 'input': 0.22; 'dec': 0.22; 'header:In-Reply- To:1': 0.22; '----------': 0.23; 'code': 0.25; 'module': 0.26; 'all,': 0.28; 'print': 0.29; 'lines': 0.30; 'line:': 0.30; 'modules,': 0.30; 'tue,': 0.32; 'there': 0.33; 'to:addr:python- list': 0.34; 'character': 0.34; 'skip:b 40': 0.34; 'yourself.': 0.34; 'regular': 0.35; 'file': 0.36; 'but': 0.37; 'could': 0.37; 'open': 0.38; 'skip:o 20': 0.38; 'skip:- 50': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'quick': 0.61; '2011': 0.61; 'efficient': 0.62; 'received:89': 0.64; 'size.': 0.71; 'spaces': 0.73; 'subject:Text': 0.73; '-0800': 0.84; '10mb': 0.84; 'start)': 0.84; '0.00': 0.91 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on jeftof X-Spam-Status: No, score=-1.0 required=7.0 tests=ALL_TRUSTED autolearn=unavailable version=3.3.1 Date: Tue, 20 Dec 2011 21:03:21 +0100 From: =?UTF-8?B?SsOpcsO0bWU=?= To: python-list@python.org Subject: Re: Text Processing In-Reply-To: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> X-Mailer: Claws Mail 3.7.10 (GTK+ 2.24.8; x86_64-pc-linux-gnu) X-Face: "kBB1-!wF@,"j_&tJ&7; T,t)PeQkZg5?.:{p,s>/,+?b6pN5!yxZy^nRXA=*?W+|J9OG!W[rdx^VA^Sx`R"g,; +MzhAq"tZFg27W4qX+ZXvLX=%piZ6c.7@oSDHyQ0Mff#HGx<{ Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324411263 news.xs4all.nl 6840 [2001:888:2000:d::a6]:58291 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17608 Tue, 20 Dec 2011 11:17:15 -0800 (PST) Yigit Turgut a =C3=A9crit: > Hi all, >=20 > I have a text file containing such data ; >=20 > A B C > ------------------------------------------------------- > -2.0100e-01 8.000e-02 8.000e-05 > -2.0000e-01 0.000e+00 4.800e-04 > -1.9900e-01 4.000e-02 1.600e-04 >=20 > But I only need Section B, and I need to change the notation to ; >=20 > 8.000e-02 =3D 0.08 > 0.000e+00 =3D 0.00 > 4.000e-02 =3D 0.04 >=20 > Text file is approximately 10MB in size. I looked around to see if > there is a quick and dirty workaround but there are lots of modules, > lots of options.. I am confused. >=20 > Which module is most suitable for this task ? You could try to do it yourself. You'd need to know what seperates the datas. Tabulation character ? Spaces ? Exemple : Input file ---------- A B C ------------------------------------------------------- -2.0100e-01 8.000e-02 8.000e-05 -2.0000e-01 0.000e+00 4.800e-04 -1.9900e-01 4.000e-02 1.600e-04 Python code ----------- # Open file with open('test1.plt','r') as f: b_values =3D [] =20 # skip as many lines as needed line =3D f.readline() line =3D f.readline() line =3D f.readline() while line: #start =3D line.find(u"\u0009", 0) + 1 #seek Tab start =3D line.find(" ", 0) + 4 #seek 4 spaces #end =3D line.find(u"\u0009", start) end =3D line.find(" ", start) b_values.append(float(line[start:end].strip())) line =3D f.readline() print b_values It gets trickier if the amount of spaces is not constant. I would then try with regular expressions. Perhaps would regexp be more efficient in any cas= e. --=20 J=C3=A9r=C3=B4me