Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将多个csv与条件合并时出错_Python_Python 3.x_Pandas_Csv - Fatal编程技术网

Python 将多个csv与条件合并时出错

Python 将多个csv与条件合并时出错,python,python-3.x,pandas,csv,Python,Python 3.x,Pandas,Csv,要求:我有69辆CSV。根据以下条件合并它们: 条件:如果标题“col7”处单元格中的值=1,则在新csv中追加相应的行 你能帮我完成这段代码吗 下面是我的代码: with open('merged.csv', 'a') as mergedFile: for csv in glob('*.csv'): if csv == 'merged.csv': pass else: for line in os.list

要求:我有69辆CSV。根据以下条件合并它们:

条件:如果标题“col7”处单元格中的值=1,则在新csv中追加相应的行

你能帮我完成这段代码吗

下面是我的代码:

with open('merged.csv', 'a') as mergedFile:
    for csv in glob('*.csv'):
        if csv == 'merged.csv':
            pass
        else:
            for line in os.listdir():
                for eachFile in open(csv, 'r'):
                    # write further code here if header 'col7' value = 1
                        # write further code here to add the corresponding rows meeting condition
                        mergedFile.write(line)
如果pandas有任何方法可以做到这一点,我们非常欢迎。

csv模块适用于此任务。您通常只是逐行筛选。熊猫在写作之前必须将整个csv记录在记忆中

import csv

# todo: do you want append, or use 'w' to start a new file?
with open('merged.csv', 'a', newline='') as mergedFile:
    writer = csv.writer(mergedFile)
    for csvFile in glob('*.csv'): # renamed variable to avoid module name collision
        if csvFile == 'merged.csv':
            continue
        with open(csvFile, newline='') as inFile:
            reader = csv.reader(inFile)
            # assuming there is a header with column names, we are looking for "col7"
            header = next(reader)
            try:
                filterCol = header.index("col7")
            except ValueError as e:
                print("no 'col7' in {}, skipping".format(csvFile))
                continue
            writer.writerows(row for row in reader if row[filterCol == "1")
csv模块适用于此任务。您通常只是逐行筛选。熊猫在写作之前必须将整个csv记录在记忆中

import csv

# todo: do you want append, or use 'w' to start a new file?
with open('merged.csv', 'a', newline='') as mergedFile:
    writer = csv.writer(mergedFile)
    for csvFile in glob('*.csv'): # renamed variable to avoid module name collision
        if csvFile == 'merged.csv':
            continue
        with open(csvFile, newline='') as inFile:
            reader = csv.reader(inFile)
            # assuming there is a header with column names, we are looking for "col7"
            header = next(reader)
            try:
                filterCol = header.index("col7")
            except ValueError as e:
                print("no 'col7' in {}, skipping".format(csvFile))
                continue
            writer.writerows(row for row in reader if row[filterCol == "1")

csv文件的名称是否合并?csv名称中的独特功能是什么?还有,你知道你的头的名字吗?如果你这样做,csv模块中的dict表单可能会有帮助,因为我要写入的新文件是merged.csv。独特的功能在这里并不重要,这更像是过滤数据。我想加上过滤器的标题名是'col7',csv文件的名称被合并了?csv名称中的独特功能是什么?还有,你知道你的头的名字吗?如果你这样做,csv模块中的dict表单可能会有帮助,因为我要写入的新文件是merged.csv。独特的功能在这里并不重要,这更像是过滤数据。我想加上过滤器的标题名是“col7”