Python strp csv行示例。向我展示您将如何处理此问题
想象一下,如果在CSV中有数千行这样的内容Python strp csv行示例。向我展示您将如何处理此问题,python,algorithm,csv,Python,Algorithm,Csv,想象一下,如果在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.476
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
# 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))
#! /usr/bin/env python
# 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))
我认为这段代码足够做这项工作
#! /usr/bin/env python
# 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))
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()))
不是吗
#! /usr/bin/env python
# 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))
编辑:对不起,我忘了“\n”
#! /usr/bin/env python
# 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))
#! /usr/bin/env python
# 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))
编辑2:不,它不起作用
#! /usr/bin/env python
# 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))
g_out.write(u for u in pat.finditer(f_in.read()))
TypeError: must be string or buffer, not generator
这个有效
#! /usr/bin/env python
# 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))
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)
或者,如果文件很大:
#! /usr/bin/env python
# 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))
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())
只是一个思维练习——每天至少一次练习想象一些特定的东西!我想象着我会问谁在雇用我,他们希望我用这样一个有数千行的csv做什么。这是Qn?拆分CSV文件行,删除GET/之前的文本和HTTP1.1之后的文本,并在简单工作表中重新组织。向我展示您的代码版本,以及如何执行此操作。。