I'm python beginner: how can I iterate over csv files in one directory and replace strings e.g.
ww into vv
.. into --
So, I do not want to replace lines having ww into vv, just those string on this line. I tried something like
#!/Python26/
# -*- coding: utf-8 -*-
import os, sys
for f in os.listdir(path):
lines = f.readlines()
But how to proceed?
import os
import csv
for filename in os.listdir(path):
with open(os.path.join(path, filename), 'r') as f:
for row in csv.reader(f):
cells = [ cell.replace('www', 'vvv').replace('..', '--')
for cell in row ]
# now you have a list of cells within one row
# with all strings modified.
Edit: Is it for you to learn/practice Python or you just need to get the job done? In the latter case, use the sed
program:
sed -i 's/www/vvv/g' yourPath/*csv
sed -i 's/\.\./,,/g' yourPath/*csv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With