how to join various bits of string and data together using python -


python newbie here. i've been working way through code create string includes date. have bits of code working data want, need formatting string tie in data together.

this have far:

def get_rectype_count(filename, rectype):     return int(subprocess.check_output('''zcat %s |  '''                                        '''awk 'begin {fs=";"};{print $6}' | '''                                        '''grep -i %r | wc -l''' %                                        (filename, rectype), shell=true))  str = "my values (" rectypes = 'click', 'bounce' myfilename in glob.iglob('*.gz'):         #print (rectypes)         print str.join(rectypes)         print (timestr)         print([get_rectype_count(myfilename, rectype)                                rectype in rectypes]) 

my output looks this:

clickmy values (bounce '2015-07-01' [222, 0] 

i'm trying create output file:

my values ('2015-07-01', click, 222) values ('2015-07-01', bounce, 0) 

when call join on string joins in sequence passed it, using separator.

>>> '123'.join(['click', 'bounce']) click123bounce 

python supports formatting strings using replacement fields:

>>> values = "my values ('{date}', {rec}, {rec_count})" >>> values.format(date='2015-07-01', rec='click', rec_count=222) "my values ('2015-07-01', click, 222)" 

with code:

for myfilename in glob.iglob('*.gz'):     rec in rectypes:         rec_count = get_rectype_count(myfilename, rec)         print values.format(date=timestr, rec=rec, rec_count=rec_count) 

edit:

if want use join, can join newline, \n:

>>> print '\n'.join(['line1', 'line2']) line1 line2 

putting together:

print '\n'.join(values.format(date=timestr,                               rec=rec,                               rec_count=get_rectype_count(filename, rec))                 filename in glob.iglob('*.gz')                 rec in rectypes) 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -