Summary grid

2016-06-22 Thread Jignesh Sutar
Say I have list of data as given in the example code below, I want to find
all the unique categories (alphabetic letters) and unique IDs (numbers) and
then produce a summary grid as manually entered in the "results". How could
I code this?

Many thanks in advance,
Jignesh


data= ["A.1", "A.2", "A.3", "B.1", "C.2", "C.3",  "D.4", "E.5", "E.6"]

cols=[]
rows=[]
for item in data:
i=item.split(".")
if i[0] not in cols: cols.append(i[0])
if i[1] not in rows: rows.append(i[1])

print cols
print rows

results=
[["Row/Col", "A", "B", "C", "D", "E"],
[1, 1, 1, 0, 0, 0],
[2, 1, 0, 1, 0, 0],
[3, 1, 0, 1, 0, 0],
[4, 0, 0, 0, 1, 0],
[5, 0, 0, 0, 0, 1],
[6, 0, 0, 0, 0, 1]]
-- 
https://mail.python.org/mailman/listinfo/python-list


Extracting parts of string between anchor points

2014-02-27 Thread Jignesh Sutar
I've kind of got this working but my code is very ugly. I'm sure it's
regular expression I need to achieve this more but not very familiar with
use regex, particularly retaining part of the string that is being
searched/matched for.

Notes and code below to demonstrate what I am trying to achieve. Any help,
much appreciated.

Examples=["Test1A",
  "Test2A: Test2B",
   "Test3A: Test3B -:- Test3C", ""]

# Out1 is just itself unless if it is empty
# Out2 is everything left of ":" (including ":" i.e. part A) and right of
"-:-" (excluding "-:-" i.e. part C)
# If text doesn't contain "-:-" then return text itself as it is
# Out3 is everything right of "-:-" (excluding "-:-" i.e. part C)
   # If text doesn't contain "-:-" but does contains ":" then return part B
only
   # If it doesn't contain ":" then return itself (unless if it empty then
"None")

for i,s in enumerate(Examples,start=1):
Out1=s if len(s)>0 else "Empty"
Out2=s[:s.find(":")+3] + s[s.find("-:-")+5:] if s.find("-:-")>0 else
s.strip() if len(s) else "Empty"
Out3=s[s.find("-:-")+4:] if s.find("-:-")>0 else
s[s.find(":")+1:].strip() if s.find(":")>0 and len(s)!=s.find(":")+1 else s
if len(s) else "Empty"
print "Item%(i)s <%(s)s>  Out1 = %(Out1)s" % locals()
print "Item%(i)s <%(s)s>  Out2 = %(Out2)s" % locals()
print "Item%(i)s <%(s)s>  Out3 = %(Out3)s" % locals()


Output:

Item1   Out1 = Test1A
Item1   Out2 = Test1A
Item1   Out3 = Test1A
Item2   Out1 = Test2A: Test2B
Item2   Out2 = Test2A: Test2B
Item2   Out3 = Test2B #INCORRECT - Should be "Test2A:
Test2B"
Item3   Out1 = Test3A: Test3B -:- Test3C
Item3   Out2 = Test3A: Test3C
Item3   Out3 = Test3C
Item4 <>  Out1 = Empty
Item4 <>  Out2 = Empty
Item4 <>  Out3 = Empty
-- 
https://mail.python.org/mailman/listinfo/python-list


Find and replace multiple RegEx search expressions

2014-03-18 Thread Jignesh Sutar
Hi,

I'm trying to delete contents of a .txt log file, matching on multiple
re.sub criteria but not sure how to achieve this.

Below is an illustration of what I am trying to achieve (of course in this
example I can combine the 3 re.sub into a single re expression but my
actual code will have a dozen plus expression I need to match on so easier
to keep them separate). Only the last re.sub will take effect in the
example below I need all 3 to take effect.


import re
o = open(r"c:\temp\outputfile.txt","w")
data = open(r"C:\Temp\infile.txt").read()
o.write( re.sub(".* ","",data) )
o.write( re.sub(".* ","",data) )
o.write( re.sub(".* ","",data) )
o.close()


Thanks in advance.
Jignesh
-- 
https://mail.python.org/mailman/listinfo/python-list