仅从列表打印中以python拆分数字并保存为CSV [英] Splitting digits only from list prints in python and save into CSV

查看:89
本文介绍了仅从列表打印中以python拆分数字并保存为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在输出(列表)中将其大号分开,然后导出为CSV

how to split the numbers in the output (the list) its large though and then export into CSV

打印(列表)输出

['id=31535&requestId=16152331&ln=143833']
['id=31539&requestId=16152331&ln=143833']
['id=31540&requestId=16152331&ln=143832']
['id=31541&requestId=16152331&ln=143831']
['id=31542&requestId=16152331&ln=143845']

推荐答案

使用正则表达式,您可以执行以下操作:

Using regex you could do this:

import re
import csv
fn = 'csvtest1.csv'
din = [['id=31535&requestId=16152331&ln=143833'],
['id=31539&requestId=16152331&ln=143833'],
['id=31540&requestId=16152331&ln=143832'],
['id=31541&requestId=16152331&ln=143831'],
['id=31542&requestId=16152331&ln=143845']]
with open(fn, 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['Col1', 'Col2', 'Col3'])
    for itm in din:
        writer.writerow(re.findall(r'\d+', str(itm)))

这将产生名为csvtest1.csv的csv格式文件,其中包含以下内容:

This would yield csv formatted file named csvtest1.csv which contains the following:

'Col1',    'Col2',      'Col3'
 31535,    16152331,   143833
 31539,    16152331,   143833
 31540,    16152331,   143832
 31541,    16152331,   143831
 31542,    16152331,   143845
​

这篇关于仅从列表打印中以python拆分数字并保存为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆