0

2 種類の CSV ファイルがあり、それらをマージしたいと考えています。そのために、各行で特定の値を見つけて、そこにある場合は削除したいと考えています。

list.Indexまたはlist.Remove関数を使用しようとしましたが、値が特定のファイルにない場合にエラーが発生します。

たとえば、2 つの行は次のとおりです (表示しやすくするために、残りの行を切り取っています)。

CSV 1950    1300    1180    48  48  400 2   PASS        0   51:31.5
CSV 3270    2500    1950    1300    1180                        48

値が「 3270」と「2500 」のセルを見つけて、2つのファイルが整列するようにしたい...その後、空のセルを削除したいので、もう一度-整列します...

これを行う方法を理解するのを手伝ってもらえますか?

ありがとう、ニムロッド。

4

2 に答える 2

1

ファイルから各値をループし、要素を削除するいくつかの条件を設定してから、値を新しい出力ファイルにマージすることをお勧めします

Step1 ファイルの読み込み

import sys
import csv
updatedlist = []
for val in csv.reader(open('input1.csv' , 'rb')) :
    print val

## val will be a list of each row , 
## So in this case you will have to 
## select the elements while you will be looping 
## then replace or removing the elements you want to remove
## and make a new updated list which you will then have 
## to append to a new output.csv file
## then do the same the to the other file and append to the output.csv file    


for Values  in  updatedlist :

##looping thru the values and then make a string of the values separated by commas
        f  = open('output.csv' , 'a')
        f.writelines(updatestring) ##updated list with a string of comma separated values 
        f.close()
于 2013-07-23T17:10:25.033 に答える