本文介绍了追加2 CSV文件列方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在的Python A B C $ C>。

Suppose I have two CSV files called A and B in Python.

A 如下:

 headerNameA1,headerNameA2
 1.12412424,1
 1,1
 1,1
 1,1

B 如下:

 headerNameB1,headerNameB2
 1,1
 1,1
 1,1
 1,1

我的目标是把 B 并添加它到 A A 则看起来像:

My objective is to take B and append it onto A so that A will then look like:

 headerNameA1,headerNameA2,headerNameB1,headerNameB2
 1,1,1.12412424,1
 1,1,1,1
 1,1,1,1
 1,1,1,1

这是另外一个问题,我问,这里的code,将采取 A B 并结合成一个 C

From another question I asked, here's code that will take A and B and combine them into a C:

 import csv
 with open('A','rb') as f1, open('B','rb') as f2, open('out.csv','wb') as w:
     writer = csv.writer(w)
     r1,r2 = csv.reader(f1),csv.reader(f2)
     while True:
         try:
             writer.writerow(next(r1)+next(r2))
         except StopIteration:
             break

不过,这个问题的目的只是添加 B A 的后面。

这是必要的,如果 A 是这样的,实在是太贵了磁盘空间,使这一个副本文件 C的大小删除之前, A 之后。

This would be necessary if the size of A is such that it is too expensive to disk space to make a copy of it as file C before deleting A afterwards.

使用os.system 称为一个bash的解决方案是可以接受的

A bash solution called through os.system is acceptable

推荐答案

您也许可以用一个命名管道脱身。你有一个Python程序运行它创建一个管道并打开它写模式。然后,它输出到的CSV文件的列明智的级联(类似于你有什么)已经......当另一个进程开始读取该文件,这将是能够消耗的数据,但没有文件实际存储在服务器上,它只是需求。当文件是消费,那么就什么也没有,任何试图访问它会阻止,直到另一个进程写入另一端。

You might be able to get away with a named pipe. You have a Python process run which creates a pipe and opens it in write mode. It then outputs to that the column wise concatenation of the CSV files (similar to what you've got) already... When another process starts reading that file, it'll be able to consume the data, but no file is actually stored on the server, it's just on demand. When the "file" is consumed, then there'll be nothing in it, and any attempt to access it will block until another process writes to the other end.

有些哑code - 将需要更多的深思熟虑,异常处理等...

Some dummy code - will need more thought out exception handling etc...:

import os
from itertools import izip

a = 'abcdef' # File A's rows
b = 'ghijkl' # File B's rows

outname = 'joined'

try:
    os.unlink(outname)
    os.mkfifo(outname)
except OSError:
    pass

with open(outname, 'w') as fout:
    for items in izip(a, b):
        fout.write(''.join(items) + '\n') # Do "real" write here instead...
    os.unlink(outname)

别的东西打开以读取模式的文件,消耗它来检索数据。这应该工作,除非这个过程必须有物理文件...

Something else opens that "file" in read mode and consumes it to retrieve the data. This should work unless that process has to have "physical files"...

这篇关于追加2 CSV文件列方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:18