使用Python在CSV的特定行上开始读取和写入 [英] Start reading and writing on specific line on CSV with Python

查看:450
本文介绍了使用Python在CSV的特定行上开始读取和写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件,如下所示:

I have a CSV file that looks like this:

COL_A,COL_B
12345,A=1$B=2$C=3$

如何读取该文件并将其写回新文件,但只是第二行(行)?
我想输出文件包含:

How do I read that file and wrote it back to a new file but just the second row (line)? I want the output file to contain:

12345,A=1$B=2$C=3$


$ b

谢谢!

Thanks!

推荐答案

以下代码读取csv,提取第二行,然后将第二行写入另一个文件。

The following reads your csv, extracts the second row, then writes that second row to another file.

with open('file.csv') as file:
    second_line = list(file)[1]

with open('out.csv', mode = 'w') as file:
    file.write(second_line)

这篇关于使用Python在CSV的特定行上开始读取和写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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