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


Groups > comp.lang.python > #86560

Picking apart a text line

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:text': 0.05; 'aggregate': 0.07; 'nested': 0.07; 'remaining': 0.07; 'subject:Picking': 0.07; 'sys': 0.07; '__name__': 0.09; 'data:': 0.09; 'guys.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'trailing': 0.09; 'def': 0.12; 'stored': 0.12; "'__main__':": 0.16; '(at)': 0.16; 'categories,': 0.16; 'columns': 0.16; 'competitor': 0.16; 'pdf)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sift': 0.16; 'skipping': 0.16; 'whitespace.': 0.16; 'thanks,': 0.17; 'bit': 0.19; 'trying': 0.19; 'file,': 0.19; 'split': 0.19; 'thoughts': 0.19; 'import': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; 'char': 0.24; 'headers': 0.24; 'skip': 0.24; 'header': 0.24; 'sort': 0.25; "i've": 0.25; 'script': 0.25; 'compare': 0.26; 'this:': 0.26; 'certain': 0.27; 'header:X-Complaints-To:1': 0.27; 'rest': 0.29; 'appreciated.': 0.29; 'characters': 0.30; 'field,': 0.30; 'originally': 0.30; 'along': 0.30; "i'm": 0.30; 'getting': 0.31; 'easier': 0.31; 'lines': 0.31; 'another.': 0.31; 'awards': 0.31; 'bunch': 0.31; 'extract': 0.31; 'location,': 0.31; 'strip': 0.31; 'file': 0.32; 'award': 0.32; 'another': 0.32; 'text': 0.33; 'open': 0.33; 'moment': 0.34; 'there,': 0.34; "i'd": 0.34; 'info': 0.35; 'something': 0.35; 'convert': 0.35; 'good.': 0.35; 'but': 0.35; 'add': 0.35; 'i.e.': 0.36; 'reports': 0.37; 'list': 0.37; 'list.': 0.37; 'starting': 0.37; 'gmail': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'bad': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'eventually': 0.60; 'up,': 0.60; 'gone': 0.61; 'full': 0.61; 'first': 0.61; 'back': 0.62; 'reach': 0.63; 'information': 0.63; 'field': 0.63; 'group,': 0.63; 'within': 0.65; 'life': 0.66; 'side': 0.67; 'iron': 0.68; 'line,': 0.68; 'results': 0.69; 'capital': 0.73; 'groups.': 0.74; 'score': 0.74; 'special': 0.74; 'ending': 0.78; 'age': 0.80; 'potentially': 0.81; 'monte': 0.84; 'pdf.': 0.84; 'pdfs': 0.84; 'ranks': 0.84; 'received:myvzw.com': 0.84; 'so...': 0.84; 'opens': 0.91; 'scores': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From memilanuk <memilanuk@gmail.com>
Subject Picking apart a text line
Date Thu, 26 Feb 2015 19:53:34 -0800
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 96.sub-70-199-139.myvzw.com
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.19300.1425009226.18130.python-list@python.org> (permalink)
Lines 91
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425009226 news.xs4all.nl 2898 [2001:888:2000:d::a6]:45442
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86560

Show key headers only | View raw


So... okay.  I've got a bunch of PDFs of tournament reports that I want 
to sift thru for information.  Ended up using 'pdftotext -layout 
file.pdf file.txt' to extract the text from the PDF.  Still have a few 
little glitches to iron out there, but I'm getting decent enough results 
for the moment to move on.

I've got my script to where it opens the file, ignores the header lines 
at the top, then goes through the rest of the file line by line, 
skipping lines if they don't match (don't need the separator lines) and 
adding them to a list if they do (and stripping whitespace off the right 
side along the way).  So far, so good.

#  rstatPDF2csv.py

import sys
import re


def convert(file):
     lines = []
     data = open(file)

     # Skip first n lines of headers
     for i in range(9):
         data.__next__()

     # Read remaining lines one at a time
     for line in data:

         # If the line begins with a capital letter...
         if re.match(r'^[A-Z]', line):

             # Strip any trailing whitespace and then add to the list
             lines.append(line.rstrip())

     return lines

if __name__ == '__main__':
     print(convert(sys.argv[1]))



What I'm ending up with is a list full of strings that look something 
like this:

['JOHN DOE                    C   T   HM   445-20*MW*   199-11*MW* 
194-5 1HM     393-16*MW*   198-9 1HM    198-11*MW*    396-20*MW* 
789-36*MW*     1234-56 *MW*',

Basically... a certain number of characters allotted for competitor 
name, then four or five 1-2 char columns for things like classification, 
age group, special categories, etc., then a score ('445-20'), then up to 
4 char for award (if any), then another score, another award, etc. etc. etc.

Right now (in the PDF) the scores are batched by one criterion, then 
sorted within those groups.  Makes life easier for the person giving out 
awards at the end of the tournament, not so much for someone trying to 
see how their individual score ranks against the whole field, not just 
their group or sub-group.  I want to be able to pull all the scores out 
and then re-sort based on score - mainly the final aggregate score, but 
potentially also on stage or daily scores.  Eventually I'd like to be 
able to calculate standardized z-scores so as to be able to compare 
scores from one event/location against another.

So back to the lines of text I have stored as strings in a list.  I 
think I want to convert that to a list of lists, i.e. split each line 
up, store that info in another list and ditch the whitespace.  Or would 
I be better off using dicts?  Originally I was thinking of how to 
process each line and split it them up based on what information was 
where - some sort of nested for/if mess.  Now I'm starting to think that 
the lines of text are pretty uniform in structure i.e. the same field is 
always in the same location, and that list slicing might be the way to 
go, if a bit tedious to set up initially...?

Any thoughts or suggestions from people who've gone down this particular 
path would be greatly appreciated.  I think I have a general 
idea/direction, but I'm open to other ideas if the path I'm on is just 
blatantly wrong.



Thanks,

Monte


-- 
Shiny!  Let's be bad guys.

Reach me @ memilanuk (at) gmail dot com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Picking apart a text line memilanuk <memilanuk@gmail.com> - 2015-02-26 19:53 -0800

csiph-web