将带行分隔符(\n)的python列表转换为文本中的多行

2024-04-25 08:47:55 发布

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

Python列表:

['date_stamp', var-Session k_1_p90,var-Session k_p50,var-Session k_1_p99,var-Session k_1_p50,var-Session k_p99,var-Session k_n,var-Session k_1_n,var-Session k_p90\n', '2018-01-27T08:00:00.000Z,11.24738,14.52714223443346,152.9584864275955,57.0742794485366,2302.579844821834,7.0030523E7,7.0046544E7,54.7883473905\n', '2018-01-28T08:00:00.000Z,19.5676560165637,14.08600105483512,146.269316836613,38.4980853428318,2360.6314485913717,6.2752528E7,6.27673E7,57.5012673249\n', '2018-01-25T08:00:00.000Z,85.2086139422115,14.12775262408616,1635.32595576107,31.8109592470369,2352.0700540932644,4.820291E7,4.8217209E7,7.8476639727\n']

当我把它打印到一个txt文件中时,它打印出来如下,每行前面有一个前导逗号,从第二行开始,我试图把这个数据转换成csv格式: enter image description here 需要以下输出:

enter image description here


Tags: 文件csv数据txt列表datesessionvar
1条回答
网友
1楼 · 发布于 2024-04-25 08:47:55

如果你的名单是这样的

list = ['date_stamp, var-Session k_1_p90,var-Session k_p50,var-Session k_1_p99,var-Session k_1_p50,var-Session k_p99,var-Session k_n,var-Session k_1_n,var-Session k_p90\n', '2018-01-27T08:00:00.000Z,11.24738,14.52714223443346,152.9584864275955,57.0742794485366,2302.579844821834,7.0030523E7,7.0046544E7,54.7883473905\n', '2018-01-28T08:00:00.000Z,19.5676560165637,14.08600105483512,146.269316836613,38.4980853428318,2360.6314485913717,6.2752528E7,6.27673E7,57.5012673249\n', '2018-01-25T08:00:00.000Z,85.2086139422115,14.12775262408616,1635.32595576107,31.8109592470369,2352.0700540932644,4.820291E7,4.8217209E7,7.8476639727\n']
 f = open('sample.txt', 'w+')
 for list_element in list:
     f.write('%s' %list_element)
 f.close()

相关问题 更多 >