user3170242 user3170242 - 1 year ago 102
Python Question

remove quotes from csv file data in python

I have a csv that is being imported from url and placed into a database, however it imports with quotes around the names and id like to remove them. The originial format of the csv file is

"Apple Inc.",113.08,113.07
"Alphabet Inc.",777.61,777.30
"Microsoft Corporation",57.730,57.720

the code I currently have is as follows.

def csv_new(conn, cursor, filename):
with open(filename, 'rt') as csv_file:
csv_data = csv.reader(csv_file)
for row in csv_data:
if(not row[0][0].isdigit()):
split = [int(x) for x in row[0].split('/')]
row[0] = datetime.datetime(split[2], split[0],
cursor.execute('INSERT INTO `trade_data`.`import_data`'
'(date, name, price) VALUES(%s, "%s", %s)',

final database looks like this

Name | Price1| Price 2|
'Apple Inc.' 113.08 113.07
'Alphabet Inc.' 777.61 777.30
'Microsoft Corporation' 57.730 57.720

and I would like it to look like

Name | Price1| Price 2|
Apple Inc. 113.08 113.07
Alphabet Inc. 777.61 777.30
Microsoft Corporation 57.730 57.720

I tried using for row in csv.reader(new_data.splitlines(), delimiter=', skipinitialspace=True): but it threw errors

Answer Source

Figured it out, the problem was as tdelaney mentioned was that the quotes were not acually in the string it was python, so my changing value in

cursor.execute('INSERT INTO `trade_data`.`import_data`'
                       '(date, name, price) VALUES(%s, "%s", %s)',

to %s instead of "%s" it fixed the problem and removed the extra quotes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download