简体   繁体   English

CSV合并问题,Python

[英]csv merging issue, python

Using the following code to merge CSV files, it will at times put the data in the wrong columns. 使用以下代码合并CSV文件,有时会将数据放入错误的列中。 Rather than being in Columns AD it will put the data in columns FJ. 而不是将其放在AD列中,而是将数据放在FJ列中。 From what I can tell is it's the first line of a new CSV that gets put in the wrong column, however, not every CSV file. 据我所知,这是新CSV的第一行,但该列放在错误的列中,但是,并不是每个CSV文件都包含在其中。

import glob
import codecs
import csv 

my_files = glob.glob("*.csv") 

header_saved = False 
with codecs.open('Final-US-Allies-Expects.csv','w', "UTF-8", 'ignore') as file_out: #save data to
    for filename in my_files:
        with codecs.open(filename, 'r', 'UTF-8', 'ignore') as file_in: 
            header = next(file_in) 
            if not header_saved: 
                file_out.write(header) #write header
                header_saved = True
            for line in file_in:
                file_out.write(line) #write next line

original code available at Merging multiple CSV files without headers being repeated (using Python) (reputation not high enough to add to original question) 合并多个CSV文件时可用的原始代码, 而无需重复标题(使用Python) (声誉不足以添加到原始问题中)

Visual of issue 视觉问题

I've attached a visual of the issue. 我附上了这个问题的图片。 I need to be able to have every line be written in in the column it is meant to be written into. 我需要能够将每一行都写入要写入的列中。

Thanks for your help in advance. 感谢您的帮助。

Looks like you are not checking if the lines end in new line character before writing it to the file. 看起来您没有在将行写入文件之前检查行是否以换行符结尾。 This could mess up the alignment. 这可能会弄乱对齐方式。 Could you try this? 你可以试试这个吗?

import glob
import codecs
import csv

my_files = glob.glob("*.csv")

header_saved = False
with codecs.open('output.csv','w', "UTF-8", 'ignore') as file_out:
    for filename in my_files:
        with codecs.open(filename, 'r', 'UTF-8', 'ignore') as file_in:
            header = next(file_in)
            if not header_saved:
                file_out.write(header if "\n" == header[-1] else header + "\n")
                header_saved = True
            for line in file_in:
                file_out.write(line if "\n" == line[-1] else line + "\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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