Batch Editing Of Csv Files With Python
I need to edit several csv files. Actually, most of the files are fine as they are, it's just the last (41st) column that needs to be changed. For every occurrence of a particular
Solution 1:
Try something along these lines. Now using the glob
module as mentioned by @SaulloCastro and the csv
module.
import glob
import csv
for item in glob.glob(".csv"):
r = list(csv.reader(open(item, "r")))
for row in r:
row[-1] = row[-1].replace("S-D", "S")
w = csv.writer(open(item, "w"))
w.writerows(r)
Solution 2:
Be sure to read up on the Python documentation for the CSV File Reading and Writing. Lots to learn there. Here is a basic example based on your question. Only modifying the data in the last column, writing out a modified file with "_edited" in the name.
import os
import csv
path=os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
if filename.endswith('.csv'):
r=csv.reader(open(filename))
new_data = []
for row in r:
row[-1] = row[-1].replace("S-D", "S")
new_data.append(row)
newfilename = "".join(filename.split(".csv")) + "_edited.csv"
with open(newfilename, "w") as f:
writer = csv.writer(f)
writer.writerows(new_data)
Post a Comment for "Batch Editing Of Csv Files With Python"