remove first 4 lines in multiple csv files python

社会主义新天地 提交于 2021-01-28 05:20:30

问题


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

that's my code:

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:])

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


回答1:


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:])



回答2:


If you want to use python you can do this.

import csv
import os

def read_csv(inputfile, outputfile):
     try:

        with open(inputfile, 'r') as csvfile:

            file = csv.reader(csvfile, delimiter='|', lineterminator = '\n')
            for i, line in enumerate(file):
            if i > 3:
                write_csv(line,outputfile)

     except IOError:
            print "IOError in ", inputfile


def write_csv(w_list, outputfile):
    with open(outputfile, 'a') as f:
        writer = csv.writer(f, delimiter = '|', lineterminator = '\n')
        writer.writerows(w_list)


def main():

    indir = 'path to dir with csv'


    for root, dirs, filenames in os.walk(indir):
        for f in filenames:
            filename = os.path.join(root , f)
            if '.csv' in filename:
                read_csv(filename, outputfile='output' +filename)


if __name__=="__main__":
    main()

Or you can use:

tail -n +4 original.csv > original-4-top-lines.csv


来源:https://stackoverflow.com/questions/43451580/remove-first-4-lines-in-multiple-csv-files-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!