删除多个csv文件python中的前4行 [英] remove first 4 lines in multiple csv files python

查看:113
本文介绍了删除多个csv文件python中的前4行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何删除CSV文件中的行,但是我想删除多个CSV文件中的多行.

I know how to remove lines in a CSV file, however looking at removing multiple lines in multiple CSV files.

那是我的代码:

import csv
import os
import glob

myfiles = glob.glob('*.csv',recursive=False)

with open(myfiles, 'r') as fin:
    data = fin.read().splitlines(True)
with open(myfiles, 'w') as fout:
    fout.writelines(data[5:])

我想实现以下目标:1)遍历当前目录.2)删除CSV文件中的前4行并将其保存.

I want to achieve the following: 1) Iterate through current directory. 2) Remove first 4 lines in a CSV file and save it.

推荐答案

答案似乎很有帮助.这是对多个文件的略微调整:

This answer looks helpful. Here is a slight tweak of it to handle multiple files:

import glob

myfiles = glob.glob('*.csv')
for file in myfiles:
    lines = open(file).readlines()
    open(file, 'w').writelines(lines[3:])

这篇关于删除多个csv文件python中的前4行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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