微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不使用熊猫的情况下按行追加两个 .csv 文件

如何解决如何在不使用熊猫的情况下按行追加两个 .csv 文件

我想附加两个具有相同列数/名称的 .csv 文件并保存输出文件。但是我不想使用pandas concat 或pandas read_csv。有没有办法使用纯 python 'with open' 来实现这一点?

解决方法

这样的事情应该可以工作:

# read first file
with open('file1.csv','r') as txt:
    lines = txt.readlines()

# extend the list with the lines from the second file
with open('file2.csv','r') as txt:
    lines.extend(txt.readlines())

# remove \n from the end of each line
lines = [line.strip() for line in lines]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。