Python strp csv rows example .Show me how you would go about with this
Imagine if you had thousands of rows of this in CSV .
maps.google.co.ke GET /maps?f=q&source=s_q&hl=en&geocode=&q=Africa&aq=0&sll=1.845384,32.871094&sspn=18.498214,20.786133&ie=UTF8&hq=&hnear=Africa&ll=-8.783195,34.508523&spn=69.476093,83.144531&z=3&output=embed HTTP/1.1 1.29646542461634E+015 168722060 1606370688 ? ken
.
#! /usr/bin/env python开发者_如何学Go
# Split the CSV and eliminate text before GET/ and text after HTTP1.1 and reorganize in simple sheet .
import csv
try:
dremel = csv.reader(open('dremel-columns.csv', 'rb'), delimiter=' ', quotechar='|')
except IOError:
print 'CSV Error'
dremel_list= list(dremel)
dremel_l = []
cw = csv.writer(open("out.txt", "wb"))
#print dremel_list
for l in dremel_list:
if l[1:-1]:
l_shout = l[1:-1]
#print l_shout
cw.writerow(l_shout)
#print "".join(l_shout)
#cw.writerow("".join(l_shout))
i think this code in enough to do the job.
import re
pat = re.compile('GET.*?HTTP1.1')
with open('dremel-columns.csv', 'rb') as f_in, open("out.txt", "wb") as g_out:
g_out.write(u + '\n' for u in pat.finditer(f_in.read()))
isn't it ?
EDIT: excuse me, I forgot the '\n'
.
EDIT 2: no, it doesn't do the job
g_out.write(u for u in pat.finditer(f_in.read()))
TypeError: must be string or buffer, not generator
This one works
import re
pat = re.compile('GET.*?HTTP1.1')
with open('dremel-columns.csv', 'rb') as f_in, open("out.txt", "wb") as g_out:
for u in pat.finditer(f_in.read()):
g_out.write(u)
Or, if the file is big:
import re
pat = re.compile('GET.*?HTTP1.1')
with open('dremel-columns.txt', 'rb') as f_in, open("out.txt", "wb") as g_out:
for line in f_in:
g_out.write(pat.search(line).group())
精彩评论