Checking the Value of a Specific Column in Every Row of a CSV with Python -
using data follows simplicity:
name,age,height joe,14,65 sam,18,74 sarah,21,62
i want go through each line of file, compare age
column value (for example: 16). if value less fixed value, want delete line.
in example, i'd left following data:
name,age,height sam,18,74 sarah,21,62
thanks in advance!
here's basic example, using csv module. creates new file less criteria data.
#!/usr/bin/python import csv infile = 'input.csv' outfile = 'output.csv' wfh = open(outfile, 'w') open(infile, 'r') fh: reader = csv.dictreader(fh, delimiter=',') row in reader: name = row['name'] age = row['age'] height = row['height'] if age >= 16: wfh.write("{},{},{}".format(name, age, height)) wfh.close()
Comments
Post a Comment