Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81972 > unrolled thread

help with processing text file

Started by"C. Ng" <ngcbmy@gmail.com>
First post2014-12-04 20:46 -0800
Last post2014-12-05 00:46 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  help with processing text file "C. Ng" <ngcbmy@gmail.com> - 2014-12-04 20:46 -0800
    Re: help with processing text file Gary Herron <gary.herron@islandtraining.com> - 2014-12-04 20:54 -0800
    Re: help with processing text file Dave Angel <davea@davea.name> - 2014-12-05 00:46 -0500

#81972 — help with processing text file

From"C. Ng" <ngcbmy@gmail.com>
Date2014-12-04 20:46 -0800
Subjecthelp with processing text file
Message-ID<d3e302d4-f4de-4307-8c72-60ed643b264d@googlegroups.com>
Hi,

Given the sample text file below (where the gibberish represent the irrelevant portions) :

....
abcddsdfffgfg
ggfhghghgfhghgh   round 5 xccdcxcfd
sdfdffdfbcvcvbbvnghg score = 0.4533 
abcddsdfffgfg     round 5 level = 0.15
ggfhghghgfhghgh   round 10 dfsdfdcdsd
sdfdffdfbcvcvbbvnghg score = 0.4213 
sdsdaawddddsds    round 10 level = 0.13
......and so on....


I would like to extract the values for round, score and level:
5 0.4533 0.15
10 0.4213 0.13
....and so on...

Please advise me how it can be done, and what Python functions are useful.

Thanks.







[toc] | [next] | [standalone]


#81973

FromGary Herron <gary.herron@islandtraining.com>
Date2014-12-04 20:54 -0800
Message-ID<mailman.16605.1417755628.18130.python-list@python.org>
In reply to#81972
On 12/04/2014 08:46 PM, C. Ng wrote:
> Hi,
>
> Given the sample text file below (where the gibberish represent the irrelevant portions) :
>
> ....
> abcddsdfffgfg
> ggfhghghgfhghgh   round 5 xccdcxcfd
> sdfdffdfbcvcvbbvnghg score = 0.4533
> abcddsdfffgfg     round 5 level = 0.15
> ggfhghghgfhghgh   round 10 dfsdfdcdsd
> sdfdffdfbcvcvbbvnghg score = 0.4213
> sdsdaawddddsds    round 10 level = 0.13
> ......and so on....
>
>
> I would like to extract the values for round, score and level:
> 5 0.4533 0.15
> 10 0.4213 0.13
> ....and so on...
>
> Please advise me how it can be done, and what Python functions are useful.
>
> Thanks.

I would use regular expressions.  See 
https://docs.python.org/3/library/re.html

Gary Herron

[toc] | [prev] | [next] | [standalone]


#81977

FromDave Angel <davea@davea.name>
Date2014-12-05 00:46 -0500
Message-ID<mailman.16609.1417758377.18130.python-list@python.org>
In reply to#81972
On 12/04/2014 11:46 PM, C. Ng wrote:
> Hi,
>
> Given the sample text file below (where the gibberish represent the irrelevant portions) :
>
> ....
> abcddsdfffgfg
> ggfhghghgfhghgh   round 5 xccdcxcfd
> sdfdffdfbcvcvbbvnghg score = 0.4533
> abcddsdfffgfg     round 5 level = 0.15
> ggfhghghgfhghgh   round 10 dfsdfdcdsd
> sdfdffdfbcvcvbbvnghg score = 0.4213
> sdsdaawddddsds    round 10 level = 0.13
> ......and so on....
>
>
> I would like to extract the values for round, score and level:
> 5 0.4533 0.15
> 10 0.4213 0.13
> ....and so on...
>
> Please advise me how it can be done, and what Python functions are useful.
>
There's lots of ambiguity in that "specification."  Can you be sure, for 
example that the gibberish does not ever include the string "round", 
"score", or "level"?

Can you be sure that the relevant 3 lines for a given record are 
adjacent, and in that order?  Do you happen to know that "round" always 
starts in a particular column?, and that "score" starts in another 
particular column?

How would you solve it by hand?  Something like the following?

OPen the file.
Skip all lines till column 19-23 contain "round"
find the first space delimited field starting in column 25, and call it 
round_num

On the next line, split the line into words, and save the last word into 
score_val

On the next line, take a substring of the line starting with column 23, 
parse it into words, and store the second word in level_num

Save the values round_num,score_val, and level_num in a tuple, or a 
string, or whatever you find useful, and append it to a result list.

Repeat till end of file.

Lots more error checking is possible, and advisable, but without knowing 
what the file really looks like, I see no point in guessing.

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web