Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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_File_Csv - Fatal编程技术网

Python-根据列对多个文件中的csv数据进行排序

Python-根据列对多个文件中的csv数据进行排序,python,file,csv,Python,File,Csv,我有一个包含多个文件的文件夹,每个文件中有不同数量的列。我想遍历目录,打开每个文件并循环遍历每一行,根据该行中的列数将该行写入一个新的CSV文件。最后,我想为所有包含14列的行创建一个大CSV,为所有包含18列的行创建另一个大CSV,并为所有其他列创建最后一个CSV 这是我到目前为止所拥有的 import pandas as pd import glob import os import csv path = r'C:\Users\Vladimir\Documents\projects\ET

我有一个包含多个文件的文件夹,每个文件中有不同数量的列。我想遍历目录,打开每个文件并循环遍历每一行,根据该行中的列数将该行写入一个新的CSV文件。最后,我想为所有包含14列的行创建一个大CSV,为所有包含18列的行创建另一个大CSV,并为所有其他列创建最后一个CSV

这是我到目前为止所拥有的

import pandas as pd
import glob
import os
import csv


path = r'C:\Users\Vladimir\Documents\projects\ETLassig\W3SVC2'
all_files = glob.glob(os.path.join(path, "*.log")) 

for file in all_files:
    for line in file:
        if len(line.split()) == 14:
            with open('c14.csv', 'wb') as csvfile:
                csvwriter = csv.writer(csvfile, delimiter=' ')
                csvwriter.writerow([line])
        elif len(line.split()) == 18:
            with open('c14.csv', 'wb') as csvfile:
                csvwriter = csv.writer(csvfile, delimiter=' ')
                csvwriter.writerow([line])          
            #open 18.csv
        else:
            with open('misc.csv', 'wb') as csvfile:
                csvwriter = csv.writer(csvfile, delimiter=' ')
                csvwriter.writerow([line])
print(c14.csv)

有人能就如何实现这一点提供任何反馈吗

您可以将所有列作为列表添加到列表中:

l = []
for file in [your_files]:
    with open(file, 'r') as f:
        for line in f.readlines()
            l.appned(line.split(" "))
现在您有了列表列表,只需使用子列表的长度对它们进行排序,然后将其放入新文件中:

l.sort(key=len)

with open(outputfile, 'w'):
     # Write  lines here as you want

您可以将所有列添加为列表中的列表:

l = []
for file in [your_files]:
    with open(file, 'r') as f:
        for line in f.readlines()
            l.appned(line.split(" "))
现在您有了列表列表,只需使用子列表的长度对它们进行排序,然后将其放入新文件中:

l.sort(key=len)

with open(outputfile, 'w'):
     # Write  lines here as you want

在此之前,请注意,您可以按原样将输入文件中的行复制到输出文件中,无需CSV机器

也就是说,我建议使用文件对象字典和字典的
get
方法,这允许指定默认值

files = {14:open('14.csv', 'wb'),
         18:open('18.csv', 'wb')}
other = open('other.csv', 'wb')

for file in all_files:
    for line in open(file):
        llen = len(line.split())
        target = files.get(llen, other)
        target.write(line)
如果您必须处理大约一百万条记录,那么请注意,因为

In [20]: a = 'a '*20                                                                      

In [21]: %timeit len(a.split())                                                           
599 ns ± 1.59 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [22]: %timeit a.count(' ')+1                                                           
328 ns ± 1.28 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
 
您应该将上面的
循环替换为

for file in all_files:
    for line in open(file):
        fields_count = line.count(' ')+1
        target = files.get(fields_count, other)
        target.write(line)
应该是因为,即使我们提到纳秒,文件系统访问也是一样的

In [23]: f = open('dele000', 'w')                                                         

In [24]: %timeit f.write(a)                                                               
508 ns ± 154 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

作为拆分/计数。

请注意,您可以按原样将行从输入文件复制到输出文件,无需CSV机器

也就是说,我建议使用文件对象字典和字典的
get
方法,这允许指定默认值

files = {14:open('14.csv', 'wb'),
         18:open('18.csv', 'wb')}
other = open('other.csv', 'wb')

for file in all_files:
    for line in open(file):
        llen = len(line.split())
        target = files.get(llen, other)
        target.write(line)
如果您必须处理大约一百万条记录,那么请注意,因为

In [20]: a = 'a '*20                                                                      

In [21]: %timeit len(a.split())                                                           
599 ns ± 1.59 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [22]: %timeit a.count(' ')+1                                                           
328 ns ± 1.28 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
 
您应该将上面的
循环替换为

for file in all_files:
    for line in open(file):
        fields_count = line.count(' ')+1
        target = files.get(fields_count, other)
        target.write(line)
应该是因为,即使我们提到纳秒,文件系统访问也是一样的

In [23]: f = open('dele000', 'w')                                                         

In [24]: %timeit f.write(a)                                                               
508 ns ± 154 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

作为拆分/计数。

非常感谢-是否有写入输出文件的writelines()方法?是的,您可以做很多事情@JamesCooperThanks非常感谢-是否有writelines()方法用于写入输出文件?是的,您可以做很多事情@詹姆斯库珀