0

I would like to make column 2 in the csv file to be all lowercase and removing all the punctuation and save the file. How can i do that?

import re
import csv

with open('Cold.csv', 'rb') as f_input1:
    with open('outing.csv', 'wb') as f_output:

      reader = csv.reader(f_input1)
      writer = csv.writer(f_output)

for row in reader:
      row[1] = re.sub('[^a-z0-9]+', ' ', str(row[1].lower()))
      writer.writerow(row)
f_input1.close()

How do i add :

re.sub('[^A-Za-z0-9]+', ' ', str(row))
filewriter.writerow([new_row.lower()]) 

or .lower in this code?

4
  • the file name is in.csv and i want to edit that. Commented Nov 9, 2017 at 14:32
  • And what does your code currently do? Commented Nov 9, 2017 at 14:34
  • Possible duplicate of Using the lowercase function with CSV rows Commented Nov 9, 2017 at 14:34
  • f_input1.close() is not required when using a with statement here. Also you would need to indent your for loop to the same level as your reader and writer lines. Commented Nov 9, 2017 at 14:58

3 Answers 3

2

You could add your code to modify your cell as follows:

import re
import csv

with open('in.csv', 'rb') as f_input, open('out.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)

    for row in csv.reader(f_input):
        row[1] = re.sub('[^A-Za-z0-9]+', '', row[1].lower())
        csv_output.writerow(row)

.lower() is used to first convert the string to lowercase. Using with ensures that your files are both automatically closed at the end.

Note, your regular expression sub should replace any invalid characters with an empty string, e.g. '', you currently have it set to be a single space.

Sign up to request clarification or add additional context in comments.

Comments

1

Just edit the row in place and write it back out

with open('Cold.csv', 'rb') as f_input1, open('outing.csv', 'wb') as f_output:

    reader = csv.reader(f_input1)
    writer = csv.writer(f_output)

    for row in reader:  
        row[1] = re.sub('[^a-z0-9]+', ' ', str(row[1].lower()))
        writer.writerow(row)

2 Comments

Do i have to close the file?
You don't need to close a file when using with open() syntax
-1

Easiest solution would be combining these two approaches in you code.

import string
s.translate(None, string.punctuation) --> to remove punctuation

##if speed is not an issue
exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)


row.lower() --> to convert to lower case

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.