Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 Python多处理:解析、编辑和编写长系列csv文件_Python 2.7_Csv_Multiprocessing - Fatal编程技术网

Python 2.7 Python多处理:解析、编辑和编写长系列csv文件

Python 2.7 Python多处理:解析、编辑和编写长系列csv文件,python-2.7,csv,multiprocessing,Python 2.7,Csv,Multiprocessing,我有一个非常长的类似cvs文件系列(总共14Gb)。我需要打开每个文件,替换某些字符,并将固定版本写入新文件。我想使用我的多核计算机的处理能力。我尝试使用mp.Pools和mp.Process/mp.Queue。池版本可以工作,但队列方法会产生以下错误: IOError:[Errno 22]无效的模式('r')或文件名:“” 这是我的池代码的简化版本: import os import pandas as pd import multiprocessing as mp def fixer(a_f

我有一个非常长的类似cvs文件系列(总共14Gb)。我需要打开每个文件,替换某些字符,并将固定版本写入新文件。我想使用我的多核计算机的处理能力。我尝试使用mp.Pools和mp.Process/mp.Queue。池版本可以工作,但队列方法会产生以下错误:

IOError:[Errno 22]无效的模式('r')或文件名:“”

这是我的池代码的简化版本:

import os
import pandas as pd
import multiprocessing as mp
def fixer(a_file):
    lines = []
    opened_file = open(a_file)
    for each_line in opened_file:
        lines.append(each_line.replace('mad', 'rational'))
    opened_file.close()
    df = pd.DataFrame(lines)
    #some pandas magics here
    df.to_csv(a_file[:-4] + '_fixed.csv')
if __name__ == "__main__":
    my_path = os.getcwd()
    my_files = list(os.walk(my_path))[0][2] #I just get the list of file names here
    processors = mp.cpu_count()
    pool = mp.Pool(processes = processors) # I set as many processes as processors my computer has.
    pool.map(fixer, my_files)
这是队列方法的一个:

import os
import pandas as pd
import multiprocessing as mp
def fixer(a_file):
    lines = []
    opened_file = open(a_file)
    for each_line in opened_file:
        lines.append(each_line.replace('mad', 'rational'))
    opened_file.close()
    df = pd.DataFrame(lines)
    #some pandas magics here
    df.to_csv(a_file[:-4] + '_fixed.csv')
if __name__ == "__main__":
    my_path = os.getcwd()
    my_files = list(os.walk(my_path))[0][2] #I just get the list of file names here
    processors = mp.cpu_count()
    queue = mp.Queue()
    for each_file in my_files:
        queue.put(each_file)
    processes = [mp.Process(target = fixer, args=(queue,)) for core in range(processors)]
    for process in processes:
        process.start()
    for process in processes:
        process.join()

如果您能提供一个示例使队列版本正常工作,我将不胜感激。在第二个处理步骤中,在写入文件之前,我需要处理器获得中间结果并进行一些计算。这就是我需要队列的原因。

队列脚本中的问题是,我没有获取队列中的下一个元素,而是将整个队列传递给fixer函数。通过将
queue.get()
的值分配给fixer函数中的变量,可以解决此问题:

import os
import pandas as pd
import multiprocessing as mp
def fixer(a_queue):
    a_file = a_queue.get()
    lines = []
    opened_file = open(a_file)
    for each_line in opened_file:
        lines.append(each_line.replace('mad', 'rational'))
    opened_file.close()
    df = pd.DataFrame(lines)
    #some pandas magics here
    df.to_csv(a_file[:-4] + '_fixed.csv')
if __name__ == "__main__":
    my_path = os.getcwd()
    my_files = list(os.walk(my_path))[0][2] #I just get the list of file names here
    processors = mp.cpu_count()
    queue = mp.Queue()
    for each_file in my_files:
        queue.put(each_file)
    processes = [mp.Process(target = fixer, args=(queue,)) for core in range(processors)]
    for process in processes:
        process.start()
    for process in processes:
        process.join()

好的,我发现了发生的事情。请参阅下面的答案。有人能解释一下为什么在定义
mp.Process
args
时,在通过
队列后需要包含
?这是因为
args
参数必须接收一个元组作为参数,并且当python中的元组只有一个元素时,它必须有一个
在该元素之后。更多信息请参见第5.3节。