I don't know if you could fix this code..
but there is something wrong with it or with me..
i don't know which..

here is the code for the .py script

import string
import sys

# input

if len(sys.argv) < 2:
    print "Not enough arguments, quitting."
    quit()

if len(sys.argv) > 3:
    print "Too many arguments, quitting."
    quit()

print "Reading from input file: %s \n" % sys.argv[1]

file=open(sys.argv[1],'r')

try:
    outarg=sys.argv[2]
except IndexError:
    outarg='att'

if outarg == 'def': outtype=1
if outarg == 'att': outtype=2
if outarg == 'both': outtype=3

#try:
#    listarg=sys.argv[2]
#    file=open(sys.argv[2],'r')
#except IndexError:
#    listarg=False

# reading in

cnt={}
cnt2={}
lastline=' '

for line in file:
    if 'sieged' in lastline or 'attacked' in lastline or ') pillaged' in lastline:
        lspl=lastline.split()

        name=lspl[3]
        mid=lspl[4].split('\'')[0]
        defender=name+' '+mid

	name=lspl[0]
        mid=lspl[1]
	attacker=name+' '+mid
		
	if ') pillaged' in lastline:
	  if len(line) < 5:		# break loop if line=blank (block, nothing killed)
	    lastline=line
	    continue
	  nspl=line.split()
	  if nspl[2]=='killed':		# break loop if blocked
	    lastline=line
	    continue

        cnt.setdefault(defender,[]).append(attacker)
        cnt2.setdefault(attacker,[]).append(defender)
        
    lastline=line


# condensing data

# sort by attacker

if outtype==2 or outtype==3:

    print "-----------------------------------"
    print 'Active counters sorted by ATTACKER:'
    print "-----------------------------------"

    for i in cnt2.iterkeys():
        cond={}
        for j in cnt2[i]:
            if cond.has_key(j):
                cond[j]=cond[j]+1
            else: cond.setdefault(j,1)
        print '\n',i,'has given counters to:'
        for k,v in cond.iteritems():
            vstr=str(v)
            print k+' ('+vstr+'x)'

    print 

# sort by defender

if outtype==1 or outtype==3:

    print "-----------------------------------"
    print 'Active counters sorted by DEFENDER:'
    print "-----------------------------------"

    for i in cnt.iterkeys():
        cond={}
        for j in cnt[i]:
            if cond.has_key(j):
                cond[j]=cond[j]+1
            else: cond.setdefault(j,1)
        print '\n',i,'has counters on:'
        for k,v in cond.iteritems():
            vstr=str(v)
            print k+' ('+vstr+'x)'
    print

print "-----------------------------------"

it will be looking for a text that looks like this

Sun May 22 15:02:36 2011
Jing (#101)'s army engaged CaptainCondom (#500)'s army on the battlefield.


Sun May 22 15:04:21 2011
Jing (#101)'s army engaged ReSet (#235)'s army on the battlefield.


Sun May 22 15:05:25 2011
A huge battle rages between the borders of Jing (#101) and HanSoloN (#529)'s country. The massive battle continues everyday, totally blocking traffic.


Sun May 22 15:12:36 2011
A huge battle rages between the borders of PieceOfMind (#71) and Jing (#101)'s country. The massive battle continues everyday, totally blocking traffic.


Sun May 22 15:13:35 2011
A huge battle rages between the borders of Jing (#101) and Meatslap (#103)'s country. The massive battle continues everyday, totally blocking traffic.


Sun May 22 15:20:24 2011
"c"
A huge battle rages between the borders of HanSoloN (#529) and Jing (#101)'s country. The massive battle continues everyday, totally blocking traffic.


Sun May 22 15:58:43 2011
"random.."
GooD (#170)'s army engaged Jing (#101)'s army on the battlefield.


Sun May 22 16:18:23 2011
"R"
wcom (#365)'s army engaged Jing (#101)'s army on the battlefield.


Sun May 22 21:02:43 2011
Ladiesman (#242)'s army engaged Jing (#101)'s army on the battlefield.


Mon May 23 00:07:40 2011
A huge battle rages between the borders of IronMan (#60) and Jing (#101)'s country. The massive battle continues everyday, totally blocking traffic.


Mon May 23 01:04:29 2011
"r"
TheBlackthorn (#523)'s army engaged Jing (#101)'s army on the battlefield.


Mon May 23 05:33:03 2011
Jing (#101)'s army engaged WackoGecko (#243)'s army on the battlefield.


Mon May 23 13:01:36 2011
Jing (#101)'s army engaged Halimar (#346)'s army on the battlefield.

then the result will be look like this
click here

thanks

if 'sieged' in lastline or 'attacked' in lastline or ') pillaged' in lastline:

is never true, as there is not those words in your input. Also the output sample has not any 'counters' as in your print statements.

I did one extract of players from the text data like this:

from pprint import pprint 
total = []
for line in open('input_text.txt'):
    result = [words.split() for words in line.split("'") if '#' in words]
    if result: total.append([(name, num.strip('()'))
                        for side in result
                       for name,num in zip(side,side[1:]) if num.startswith('(')])
pprint(sorted(total))

Output:

[[('GooD', '#170'), ('Jing', '#101')],
 [('HanSoloN', '#529'), ('Jing', '#101')],
 [('IronMan', '#60'), ('Jing', '#101')],
 [('Jing', '#101'), ('CaptainCondom', '#500')],
 [('Jing', '#101'), ('Halimar', '#346')],
 [('Jing', '#101'), ('HanSoloN', '#529')],
 [('Jing', '#101'), ('Meatslap', '#103')],
 [('Jing', '#101'), ('ReSet', '#235')],
 [('Jing', '#101'), ('WackoGecko', '#243')],
 [('Ladiesman', '#242'), ('Jing', '#101')],
 [('PieceOfMind', '#71'), ('Jing', '#101')],
 [('TheBlackthorn', '#523'), ('Jing', '#101')],
 [('wcom', '#365'), ('Jing', '#101')]]

Edited 5 Years Ago by pyTony: sort, pprint

And from that:

from pprint import pprint 
from itertools import groupby

total = []
for line in open('input_text.txt'):
    result = [words.split() for words in line.split("'") if '#' in words]
    if result: total.append([(name, num.strip('()'))
                        for side in result
                       for name, num in zip(side,side[1:]) if num.startswith('(')])
total.sort()
pprint(total)

grouped = {}
for key,group in groupby(total, key=lambda x: x[0]):
    grouped[key] = tuple(b for a,b in group)
    
print('-'*80)

pprint(grouped)

Output:

[[('GooD', '#170'), ('Jing', '#101')],
 [('HanSoloN', '#529'), ('Jing', '#101')],
 [('IronMan', '#60'), ('Jing', '#101')],
 [('Jing', '#101'), ('CaptainCondom', '#500')],
 [('Jing', '#101'), ('Halimar', '#346')],
 [('Jing', '#101'), ('HanSoloN', '#529')],
 [('Jing', '#101'), ('Meatslap', '#103')],
 [('Jing', '#101'), ('ReSet', '#235')],
 [('Jing', '#101'), ('WackoGecko', '#243')],
 [('Ladiesman', '#242'), ('Jing', '#101')],
 [('PieceOfMind', '#71'), ('Jing', '#101')],
 [('TheBlackthorn', '#523'), ('Jing', '#101')],
 [('wcom', '#365'), ('Jing', '#101')]]
--------------------------------------------------------------------------------
{('GooD', '#170'): (('Jing', '#101'),),
 ('HanSoloN', '#529'): (('Jing', '#101'),),
 ('IronMan', '#60'): (('Jing', '#101'),),
 ('Jing', '#101'): (('CaptainCondom', '#500'),
                    ('Halimar', '#346'),
                    ('HanSoloN', '#529'),
                    ('Meatslap', '#103'),
                    ('ReSet', '#235'),
                    ('WackoGecko', '#243')),
 ('Ladiesman', '#242'): (('Jing', '#101'),),
 ('PieceOfMind', '#71'): (('Jing', '#101'),),
 ('TheBlackthorn', '#523'): (('Jing', '#101'),),
 ('wcom', '#365'): (('Jing', '#101'),)}
>>>

If add to enermy to both sides:

from pprint import pprint 
from itertools import groupby

total = []
for line in open('input_text.txt'):
    result = [words.split() for words in line.split("'") if '#' in words]
    if result: total.append(tuple((name, num.strip('()'))
                        for side in result
                       for name, num in zip(side,side[1:]) if num.startswith('(')))
# symmetric a -> b => b -> a
total += [tuple(reversed(pair)) for pair in total]
total.sort()
pprint(total)

grouped = {}
for key,group in groupby(total, key=lambda x: x[0]):
    grouped[key] = tuple(b for a,b in group)
    
print('-'*80)

pprint(grouped)
[(('CaptainCondom', '#500'), ('Jing', '#101')),
 (('GooD', '#170'), ('Jing', '#101')),
 (('Halimar', '#346'), ('Jing', '#101')),
 (('HanSoloN', '#529'), ('Jing', '#101')),
 (('HanSoloN', '#529'), ('Jing', '#101')),
 (('IronMan', '#60'), ('Jing', '#101')),
 (('Jing', '#101'), ('CaptainCondom', '#500')),
 (('Jing', '#101'), ('GooD', '#170')),
 (('Jing', '#101'), ('Halimar', '#346')),
 (('Jing', '#101'), ('HanSoloN', '#529')),
 (('Jing', '#101'), ('HanSoloN', '#529')),
 (('Jing', '#101'), ('IronMan', '#60')),
 (('Jing', '#101'), ('Ladiesman', '#242')),
 (('Jing', '#101'), ('Meatslap', '#103')),
 (('Jing', '#101'), ('PieceOfMind', '#71')),
 (('Jing', '#101'), ('ReSet', '#235')),
 (('Jing', '#101'), ('TheBlackthorn', '#523')),
 (('Jing', '#101'), ('WackoGecko', '#243')),
 (('Jing', '#101'), ('wcom', '#365')),
 (('Ladiesman', '#242'), ('Jing', '#101')),
 (('Meatslap', '#103'), ('Jing', '#101')),
 (('PieceOfMind', '#71'), ('Jing', '#101')),
 (('ReSet', '#235'), ('Jing', '#101')),
 (('TheBlackthorn', '#523'), ('Jing', '#101')),
 (('WackoGecko', '#243'), ('Jing', '#101')),
 (('wcom', '#365'), ('Jing', '#101'))]
--------------------------------------------------------------------------------
{('CaptainCondom', '#500'): (('Jing', '#101'),),
 ('GooD', '#170'): (('Jing', '#101'),),
 ('Halimar', '#346'): (('Jing', '#101'),),
 ('HanSoloN', '#529'): (('Jing', '#101'), ('Jing', '#101')),
 ('IronMan', '#60'): (('Jing', '#101'),),
 ('Jing', '#101'): (('CaptainCondom', '#500'),
                    ('GooD', '#170'),
                    ('Halimar', '#346'),
                    ('HanSoloN', '#529'),
                    ('HanSoloN', '#529'),
                    ('IronMan', '#60'),
                    ('Ladiesman', '#242'),
                    ('Meatslap', '#103'),
                    ('PieceOfMind', '#71'),
                    ('ReSet', '#235'),
                    ('TheBlackthorn', '#523'),
                    ('WackoGecko', '#243'),
                    ('wcom', '#365')),
 ('Ladiesman', '#242'): (('Jing', '#101'),),
 ('Meatslap', '#103'): (('Jing', '#101'),),
 ('PieceOfMind', '#71'): (('Jing', '#101'),),
 ('ReSet', '#235'): (('Jing', '#101'),),
 ('TheBlackthorn', '#523'): (('Jing', '#101'),),
 ('WackoGecko', '#243'): (('Jing', '#101'),),
 ('wcom', '#365'): (('Jing', '#101'),)}
>>>

Edited 5 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.