简体   繁体   中英

combining two csv files python

I have data stored in two different CSV files. I want to dump file b at the end of file a and also I want to remove the header ie, first line of file b. I can combine two files using open('final.csv', 'a') but that also includes header of file b. Any help will be greatly appreciated.

I'm assuming you want to know how to skip the header when reading a file, since you don't specify how exactly the two files are to be appended (in-memory, on the file system, ... ?).

After opening the file, you can use next() on the file object to skip ahead one line, like so:

with open("file_b", "r") as fb:
    next(fb) # skip 1 line
    for line in fb:
        # do whatever you want with the remaining lines, e.g. append them
        # to file_a

Alternatively, because you had "numpy" as a question tag earlier, you can use numpy's loadtxt() function, which has a parameter called skiprows that can be used to do what you want. Just open file_b like so:

with open("file_b", "r") as fb:
    all_lines_except_header = numpy.loadtxt(fb, skiprows=1)

This will also parse the CSV file, however. If you're only interested in lines and not in individual fields, I would recommend the first method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM