How to Delete Lines from CSV File Using Python

Deleting rows with Python in a CSV file

You are very close; currently you compare the row[2] with integer 0, make the comparison with the string "0". When you read the data from a file, it is a string and not an integer, so that is why your integer check fails currently:

row[2]!="0":

Also, you can use the with keyword to make the current code slightly more pythonic so that the lines in your code are reduced and you can omit the .close statements:

import csv
with open('first.csv', 'rb') as inp, open('first_edit.csv', 'wb') as out:
writer = csv.writer(out)
for row in csv.reader(inp):
if row[2] != "0":
writer.writerow(row)

Note that input is a Python builtin, so I've used another variable name instead.


Edit: The values in your csv file's rows are comma and space separated; In a normal csv, they would be simply comma separated and a check against "0" would work, so you can either use strip(row[2]) != 0, or check against " 0".

The better solution would be to correct the csv format, but in case you want to persist with the current one, the following will work with your given csv file format:

$ cat test.py 
import csv
with open('first.csv', 'rb') as inp, open('first_edit.csv', 'wb') as out:
writer = csv.writer(out)
for row in csv.reader(inp):
if row[2] != " 0":
writer.writerow(row)
$ cat first.csv
6.5, 5.4, 0, 320
6.5, 5.4, 1, 320
$ python test.py
$ cat first_edit.csv
6.5, 5.4, 1, 320

Remove rows from .csv file using python

You can also do this:

lines = list()
remove= [1,2,3,4]

with open('dataset1.csv', 'r') as read_file:
reader = csv.reader(read_file)
for row_number, row in enumerate(reader, start=1):
if(row_number not in remove):
lines.append(row)

with open('new_csv.csv', 'w') as write_file:
writer = csv.writer(write_file)
writer.writerows(lines)

Remove rows from CSV file containing certain characters

I would actually not use the csv package for this goal. This can be achieved easily using standard file reading and writing.

Try this code (I have written some comments to make it self-explanatory):

# We open the source file and get its lines
with open('input_csv_file.csv', 'r') as inp:
lines = inp.readlines()

# We open the target file in write-mode
with open('purged_csv_file.csv', 'w') as out:
# We go line by line writing in the target file
# if the original line does not include the
# strings 'py-board' or 'coffee'
for line in lines:
if not 'py-board' in line and not 'coffee' in line:
out.write(line)

How to delete rows from a CSV file when I just know the row number?

There is no special function for that since that can be done with a simple for loop.

Here we have an input file which is read line by line while skipping the lines with the row number contained in rownumbers_to_remove.
Notice the enumerate(...) with the start=1 parameter to make the index start at 1 instead of 0. This might improve readability a bit.

lines = list()
rownumbers_to_remove= [5,6]

with open('name-of-file.csv', 'r') as read_file:
reader = csv.reader(read_file)
for row_number, row in enumerate(reader, start=1):
if(row_number not in rownumbers_to_remove):
lines.append(row)

with open('name-of-file.csv', 'w') as write_file:
writer = csv.writer(write_file)
writer.writerows(lines)

How to delete only one row in CSV with python

This worked for me: you could write the contents of the csv file to a list, then edit the list in python, then write the list back to the csv file.

lines = list()
memberName = input("Please enter a member's name to be deleted.")
with open('mycsv.csv', 'r') as readFile:
reader = csv.reader(readFile)
for row in reader:
lines.append(row)
for field in row:
if field == memberName:
lines.remove(row)

with open('mycsv.csv', 'w') as writeFile:
writer = csv.writer(writeFile)
writer.writerows(lines)

How to remove a range of lines from csv file?

You can use csv library for doing this.

import csv
file=open("Database.csv",'rb')
final_file=open("Database_edited",'wb')
writer=csv.writer(final_file)
line_no=1 # for knowing the line number
for row in csv.reader(file):
if(line_no<=3 or line_no>=15):
writer.writerow(row)
line_no=line_no+1
file.close()
final_file.close()

This way Database_edited will have your required file

How to delete specific data from single line in CSV FILE and last line?

Use the csv module to read and rewrite back without the last 3 columns

for file in myfiles:
rows = []

with io.open(file,"r",encoding="utf-8") as f:
reader = csv.reader(f, delimiter=",", quotechar='"')

for row in reader:
rows.append(row[:-3])

with io.open(file,"w",encoding="utf-8") as f:
writer = csv.writer(f)

for row in rows:
writer.writerow(row)

How to delete lines from csv file using python?

To filter out the last entry for groups of NameOfClass, you can make use of Python's groupby() function to return lists of rows with the same NameOfClass. The last entry from each can then be written to a file.

from itertools import groupby
import csv

with open('data_in.csv', newline='') as f_input, open('data_out.csv', 'w', newline='') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)

for key, rows in groupby(csv_input, key=lambda x: x[0]):
csv_output.writerow(list(rows)[-1])

For the data you have given, this would give you the following output:

NameOfClass,LazyClass,ComplexClass,LongParameterList,FeatureEnvy,LongMethod,BlobClass,MessageChain,RefusedBequest,SpaghettiCode,SpeculativeGenerality
com.nirhart.shortrain.MainActivity,NaN,NaN,NaN,NaN,NaN,NaN,1,NaN,NaN,NaN
com.nirhart.shortrain.path.PathParser,NaN,1,2,1,1,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.PathPoint,1,NaN,1,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.TrainPath,NaN,NaN,NaN,1,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.rail.RailActionActivity,NaN,NaN,NaN,1,1,NaN,NaN,NaN,NaN,NaN


Related Topics



Leave a reply



Submit