Python strp csv行示例。演示如何使用它

2024-05-15 14:50:29 发布

您现在位置:Python中文网/ 问答频道 /正文

想象一下,如果在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

一。在

^{pr2}$

Tags: csvsource内容getgooglehlenmaps
1条回答
网友
1楼 · 发布于 2024-05-15 14:50:29

我认为这个代码足够做这项工作。在

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()))

不是吗?在

编辑:对不起,我忘了'\n'

一。在

编辑2:不,它不起作用

^{pr2}$

这个有效

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)

或者,如果文件很大:

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())

相关问题 更多 >