Here is a very basic sample of what I'm looking to do I want to delete a row (or set of rows, using a list) with the DictWriter library but I don't know the syntax for row deletion This is what I have so far
import csv
filename = "sampletable.csv"
with open(filename, 'r') as csvfile:
csvreader = csv.DictReader(csvfile)
with open("samplerowdeletion.csv", "w") as newfile:
fieldnames = ["high","low","precipitation"]
csvwriter = csv.DictWriter(newfile, fieldnames=fieldnames)
csvwriter.writeheader()
for row in csvreader:
del(row[1])
Reading from one csv and writing some of the rows to another csv is an example of filtering . The easiest way to do this is by skipping the rows that you don't want:
for row in reader:
if row['high'] == '10':
# go back to the top of the loop
continue
writer.writerow(row)
Instead of hardcoding the check, you could write a function that will return True
if a row should be kept and False
otherwise (this kind of function is known as a predicate ):
def is_ok(row):
if row['high'] in list_of_bad_values:
return False
return True
for row in reader:
if not is_ok(row):
# go back to the top of the loop
continue
writer.writerow(row)
You avoid the explicit for
loop by using Python's filter built-in function, or a list comprehension :
def is_ok(row):
# Shorter version
return not (row['high'] in list_of_bad_values)
# Using filter
good_rows = filter(is_ok, reader)
writer.writerows(list(good_rows))
# Using a list comprehension
good_rows = [row for row in reader if is_ok(row)]
writer.writerows(rows)
If you want to filter by row number, you can use the enumerate built-in function. enumerate
will generate a row number for each row, starting at zero - you can adjust this by passing a start
argument:
enumerate(some_iterable, start=1)
import csv
ROWS_TO_DELETE = [3, 4]
def is_not_ok(row_number):
return row_number in ROWS_TO_DELETE
filename = "sampletable.csv"
with open(filename, 'r', newline=") as csvfile:
csvreader = csv.DictReader(csvfile)
with open("samplerowdeletion.csv", "w", newline=") as newfile:
fieldnames = ["high","low","precipitation"]
csvwriter = csv.DictWriter(newfile, fieldnames=fieldnames)
csvwriter.writeheader()
for row_number, row in enumerate(csvreader):
if is_not_ok(row_number):
continue
writer.writerow(row)
Alternatively, you could make a row counter variable and manually increment it
filename = "sampletable.csv"
with open(filename, 'r', newline=") as csvfile:
csvreader = csv.DictReader(csvfile)
with open("samplerowdeletion.csv", "w", newline=") as newfile:
fieldnames = ["high","low","precipitation"]
csvwriter = csv.DictWriter(newfile, fieldnames=fieldnames)
csvwriter.writeheader()
row_number = 0
for row in csvreader:
if row_number in ROWS_TO_DELETE:
continue
writer.writerow(row)
row_number += 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.