2

I am able to change the data to lowercase and remove all the punctuation but I have trouble saving the corrected data in CSV file.

import csv
import re
import os

input_file=raw_input("Name of the CSV file:")
output_file=raw_input("Output Name:")


reg_test=input_file
result = ''

with open(input_file,'r') as csvfile:
  with open(output_file,'w') as csv_out_file:
  filereader = csv.reader(csvfile)
  filewriter =csv.writer(csv_out_file)
  for row in filereader:
     row = re.sub('[^A-Za-z0-9]+', '', str(row))
     result += row + ','

lower = (result).lower()
csvfile.close()
csv_out_file.close()
2
  • 2
    Your indentation is wrong and you kind of misunderstood the way the with statement works. Commented Nov 5, 2017 at 14:58
  • You don't have to nest with statements like that. Commented Nov 5, 2017 at 15:00

1 Answer 1

2

You do not have to close the files, this is done automatically after the context of the with statement is over and you have to actually write something after you create the csv.writer, e.g. with writerow:

import csv
import re

input_file = 'in.csv'
output_file = 'out.csv'

with open(input_file, 'r') as csvfile, open(output_file, 'w') as csv_out_file:
    filereader = csv.reader(csvfile)
    filewriter = csv.writer(csv_out_file)
    for row in filereader:
        new_row = re.sub('[^A-Za-z0-9]+', '', str(row))  # manipulate the row
        filewriter.writerow([new_row.lower()])  # write the new row to the out file

# the files are closed automatically after the context of the with statement is over

This saves the manipulated content of the first csv file to the second.

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

11 Comments

Thank you it works! Where should i put if i want to remove punctuation and change all texts to lowercase in CSV/
Add your code to the line with the comment # do something with that row
i tried. but the result will string each letters. for example it should be yellow but it is shown as "Y , E ,L,L,O,W"
Do you mind edit the code telling how should i put .lower() and row = re.sub('[^A-Za-z0-9]+', '', str(row))
Thank you so much! Really appreciate it!
|

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.