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

泪湿孤枕 提交于 2020-04-11 07:57:25

问题


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() 

回答1:


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)



回答2:


Thanks for the help from everyone - below is the code that I got to receive my result. I used help(string.split) in order to get my data to appear in the correct columns after the replacement. Also, the key was making line a string, in order to do the replacement.

import csv
import string

input_file = open('DesktopData.csv', 'r')
output_file = open('fixformat.csv', 'w')
data = csv.reader(input_file)
writer = csv.writer(output_file,quoting=csv.QUOTE_ALL)# dialect='excel')
specials = '%'

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

input_file.close()
output_file.close()


来源:https://stackoverflow.com/questions/17176542/remove-specific-character-from-a-csv-file-and-rewrite-to-a-new-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!