Remove specific character from a csv file, and rewrite to a new file

Dan picture Dan · Jun 18, 2013 · Viewed 21.7k times · Source

I have a VBA macro pulling stock data every 5 minutes on the entire NYSE. This pulls everything from current price, to earnings dates, to p/e, among other metrics. I'm now trying to set up a cronjob to run a little python code to clean up the csv data before I import it into the mySQL database. Before I can set up the cronjob, I need to get the python code working... Baby steps :).

I went away from python a few years ago, but am trying to use it again here. After some research, it's been vetted that back in 2.6 they removed many of the string methods, followed by the maketrans() argument in 3.1. I'm running python 3.3.

Does anyone have any suggestions for my code below? I'm pulling in 1 line of the csv file at a time, and trying to replace the percent sign (%), with nothing (''). I've tried using the replace() argument within an if statement, but that's not working either. Many, many thanks in advance.

Thanks,

Dan

import csv
import string

input_file = open('StockData.csv', 'r')
output_file = open('Output.csv', 'w')
data = csv.reader(input_file)
writer = csv.writer(output_file)
specials = '%'

for line in data:
    trans = s.makestrans(specials, ''*len(specials))
    new_line = line.translate(trans)
    writer.writerow(new_line)

input_file.close()
output_file.close() 

Answer

rafello picture rafello · Jan 28, 2016

I know this is an old one, but I've been struggling with a similar problem.

Instead of turning each row into a string so that you can use replace, you can use a list comprehension.

So:

for line in data:
    line = str(line)
    new_line = str.replace(line,specials,'')
    writer.writerow(new_line.split(','))

becomes:

for line in data:
    line = [value.replace(specials, '') for value in line]
    writer.writerow(line)