Sunday, 15 June 2014

python - Remove line pattern -



python - Remove line pattern -

i need remove line csv specific pattern

this how csv file looks.

lbm,16730,0 namd,16733,6 namd,16731,2 namd,16732,4

if want remove line pattern 16730 , output rest of file is..

so, output this:

namd,16733,6 namd,16731,2 namd,16732,4

how do that?

here little script wrote help of files on internet

def delete_line(dello): opener = open(input_csv, 'rb') dataset = csv.reader(opener, delimiter=',') output = [] line in dataset: if 'dello' == line[1]: print line[1] #i dont know how strip here output.append(line) opener.close() fn = input_csv f = open(fn,'w') f.writelines(output) f.close()

any hints going wrong?

if need python, utilize this:

def delete_line(dello): info = open("abc.csv").readlines() = 0 line in data: if dello in line: data.pop(i) += 1 open("abc.csv", "w").write("".join(data)) delete_line("16732")

input:

lbm,16730,0 namd,16733,6 namd,16731,2 namd,16732,4

output:

lbm,16730,0 namd,16733,6 namd,16731,2

note: remove entries matching string.

update

modifying code:

import csv def delete_line(dello): opener = open("abc.csv", 'rb') dataset = csv.reader(opener, delimiter=',') output = [] line in dataset: # add together output if not matching string if dello != line[1]: # need bring together line list output.append(",".join(line) + "\n") opener.close() fn = "abc.csv" f = open(fn,'w') f.writelines(output) f.close() delete_line("16730")

if need strip out entry, can utilize dataset.pop(index).

python csv

No comments:

Post a Comment