2016-06-24 9 views
0

に応じて、csvファイルの一部の列をマージ:私はこのようになりますcsvファイル、持っている状態

5005284;5003485;C1; C2;A00.00;10-11-01;NULL;1;; 
2006483;2003855;this is some text; and some 787; or even &[]\><;A87.03;30-09-86;NULL;1; 
2006485;2003855;C;K86.00;31-12-91;NULL;1;;; 

ファイルを分離;で、残念ながら、彼らは余分な列を作成する第三列でこの文字を使用。これらのすべてを列を1つの列に連結し、以下の期待される出力に示すようにします。

これまでのところ、私が持っている:

に結果の
import re 
import pandas as pd 

text = open ('testepisodes.csv') 
cleared = pd.DataFrame() 

for line in text: 
# get rid of extra ;;; or ;; 
    line.replace(";;;", ";") 
    line.replace(";;", ";") 
    print line 
    index = line.count(";") 
    print index 
    if index==9: 
     line = re.sub(r'^((?:[^.]*\;){4}[^.]*)\..*', r'\1', line) 
    if index==8: 
     line = re.sub(r'^((?:[^.]*\;){3}[^.]*)\..*', r'\1', line) 
print line 

私がしたいのですが
2078915;2003855;this is some text; and some 787; or even &[]\><;A87.03;30-09-86;NULL;1; 
126 
126 
2078915;2003855;this is some text; and some 787; or even &[]\><;A87.03;30-09-86;NULL;1; 

commentから編集
5005284;5003485;C1 C2;A00.00;10-11-01;NULL;1; 
2006483;2003855;this is some text and some 787 or even &[]\><;A87.03;30-09-86;NULL;1; 
2006485;2003855;C;K86.00;31-12-91;NULL;1; 

インデックス2は、常に一緒にすべきものの出発点です。新しいインデックス3には 'A00.00'パターンが含まれている必要があります。 'A'は大文字(A〜Z)を表し、 '0'は数字(0〜9)を表します。

+0

実際に何を言いたいのですか?複数のインスタンスをすべて削除するかどうか ";"ファイルに書き込みますか? –

+0

また、私は問題を再現できません。私は 'print index'を' 9'としています。 – oxalorg

+0

Cはインデックス2外に出現します(C列を呼び出した場合、スプレッドシートプログラムで使用されます)。 –

答えて

2

このコードを試してみてください。このような入力ファイルで

import csv 
import re 


with open('file.csv') as f, open('newfile.csv', 'w', newline='') as newf: 
    reader = csv.reader(f, delimiter=';') 
    writer = csv.writer(newf, delimiter=';') 

    rows = [] 
    for row in reader: 
     for i, e in enumerate(row): 
      if re.match('[A-Z][0-9]{2}\.[0-9]{2}', e): # looking for pattern "A00.00" 
       idx = i 
       break 
     newrow = row[:2] 
     newrow.append(' '.join(e.strip() for e in row[2:idx])) # joining columns that should be together 
     newrow += row[idx:] 
     writer.writerow([e for e in newrow if e]) # writing only non-empty columns 


with open('newfile.csv') as f: 
    for row in f: 
     print(row) 

を:

5005284;5003485;C1 C2;A00.00;10-11-01;NULL;1 
2006483;2003855;this is some text and some 787 or even &[]\><;A87.03;30-09-86;NULL;1 
2006485;2003855;C;K86.00;31-12-91;NULL;1 

注意がいる:

5005284;5003485;C1; C2;A00.00;10-11-01;NULL;1;; 
2006483;2003855;this is some text; and some 787; or even &[]\><;A87.03;30-09-86;NULL;1; 
2006485;2003855;C;K86.00;31-12-91;NULL;1;;; 

出力ファイルは、このようなルックスを作成いいえ; csvファイルの通常の場合である各行の終わりに。ただし、必要に応じて、新しいファイルに書き込む間に各行の最後に空の列を追加します。多分このように:

writer.writerow([e for e in newrow if e] + ['']) 
0

";"

line.replace()元の行は変更されず、要求された変更が加えられた新しい行が返されます。

line.replace(";;;", ";") 
line.replace(";;", ";") 

例:

a 
Out[20]: ';fsdfds;dsfss;f;sdfsdf;sdf' 

a.replace("s", "S") 
Out[21]: ';fSdfdS;dSfSS;f;SdfSdf;Sdf' 

a 
Out[22]: ';fsdfds;dsfss;f;sdfsdf;sdf' 

ではなく、このような何かを試してみてください:

while ";;" in line: 
    line = line.replace(";;", ";") 

これは、任意の重複を削除します文書here。したがって、このコードは、あなたがそれだと思う何をしていませんを参照してください。 ";"キャラクター。試してみてください

with open("new_document.csv") as new: 
    new.write(modified_lines) 

構造を改善するようなものを

を.csvに書き込み

これを行うには良い方法はcsvファイル内の行をフィルタリングし、修正するために発電機を使用することです

それを反復して新しいファイルに書き込みます。例:

def fix_wonky_csv(wonky_csv): 
    for line in wonky_csv: 
     # fix the lines 
     yield line 

def create_new_file: 
    newfile = open(new_title, "w") 
    with open(filename) as f: 
     for line in fix_wonky_csv(f): 
      newfile.write(line) 
    newfile.close() 
+0

彼は、インデックス2からxまでのリストインデックス2の情報を連結したいと考えています。これは代替質問ではありません –

関連する問題