Reputation: 2456
I need to remove a line from a csv with a specific pattern
this is how my csv file looks.
lbm,16730,0
namd,16733,6
namd,16731,2
namd,16732,4
If I want to remove the line with the pattern 16730
and output the rest of the file as it is..
so, the output something like this:
namd,16733,6
namd,16731,2
namd,16732,4
how do I do that?
here is a small script I wrote with the help of some files on the internet
def delete_line(dello):
opener = open(input_csv, 'rb')
dataset = csv.reader(opener, delimiter=',')
output = []
for line in dataset:
if 'dello' == line[1]:
print line[1]
#i dont know how to strip it here
output.append(line)
opener.close()
fn = input_csv
f = open(fn,'w')
f.writelines(output)
f.close()
any hints where I am going wrong?
Upvotes: 1
Views: 1156
Reputation: 438
If you opt to follow uʍop ǝpısdn's suggestion and go for grep, just this will work:
grep -v ",16370," path/to/file > path/to/new_file
Assuming that the structure of the file is like the one in the sample and is consistent in all the file, though...
Just FYI, in grep, -v denotes the inverse match, returning all the results which do not contain the pattern specified.
EDIT: If you need to preserve the original file, you can use a temporary and then restore its name to the original one:
grep -v ",16370," path/to/file > path/to/new_file && rm path/to/file && mv path/to/new_file path/to/file
Upvotes: 1
Reputation: 36000
If you need python, then use this:
def delete_line(dello):
data = open("abc.csv").readlines()
i = 0
for line in data:
if dello in line:
data.pop(i)
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: this will remove all the entries matching the string.
Update
Modifying your code:
import csv
def delete_line(dello):
opener = open("abc.csv", 'rb')
dataset = csv.reader(opener, delimiter=',')
output = []
for line in dataset:
# Add to output only if not matching the string
if dello != line[1]:
# Need join as line is a list
output.append(",".join(line) + "\n")
opener.close()
fn = "abc.csv"
f = open(fn,'w')
f.writelines(output)
f.close()
delete_line("16730")
If you need to strip out an entry, you can use dataset.pop(index)
.
Upvotes: 1
Reputation: 40773
The first problem:
if 'dello' == line[1]:
dello
with quoteTherefore, the correct test should be:
if dello != line[1]:
The second problem: you read the file as a CSV, but write it out as an ordinary file. You should be consistent by either read and write as an ordinary text file, or as CSV. Mixing them up makes it hard to get the right output.
The third is not really a problem, but a suggestion: don't hard code the file name, pass it into your function. That way, your function is more versatile.
Here is my proposed code:
def delete_line(input_csv, dello):
with open(input_csv, 'rb') as f:
csv_reader = csv.reader(f)
output = []
for line in csv_reader:
if dello not in line:
output.append(line)
with open(input_csv, 'wb') as f:
csv_writer = csv.writer(f)
csv_writer.writerows(output)
Upvotes: 0
Reputation: 12098
there are two ways to run a python script like that:
first: adding a main section to the file like:
if __name__ == "__main__":
delete_line(some_paramter)
and then running from the command line:
python scriptfilename.py
or from python shell:
from scriptfilename import delete_line
delete_line(some_parameter)
do you use one of those? a script doesn't run itself.
some more unclarified business with your script:
what exactly is the input variable dello used in your script. there is a line using the string 'dello' but not the parameter dello. what did you try to do here
are you running it in the same folder with the input_csv file. is input_csv the full file name or is it input_csv.csv.
you are using input_csv as a variable, which is empty and would fail, if this isn't a variable holding the file name (from some place else in the file) you should call the file name as a string: 'input_csv.csv'
you commented: #i dont know how to strip it here? what exactly is the question and what do you mean by that? using strip(). something else?
did your script import csv before the function? if not.. nothing would work.
It is always more recommanded to work with with
when working with files. handles exceptions and automatic file closing. you can read about that in numerous places.
something like:
with open('file.csv','rb') as f:
dataset = csv.reader(f)
#the rest. and you don't need closing etc..'
Upvotes: 0