user3170242
user3170242

Reputation: 107

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()):
                continue
            split = [int(x) for x in row[0].split('/')]
            row[0] = datetime.datetime(split[2], split[0],
                                       split[1]).date().isoformat()
            print(row);
            cursor.execute('INSERT INTO `trade_data`.`import_data`'
                           '(date, name, price) VALUES(%s, "%s", %s)',
                           row)
    conn.commit()

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

Upvotes: 0

Views: 1133

Answers (2)

user3170242
user3170242

Reputation: 107

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)',
                       row)

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

Upvotes: 1

tdelaney
tdelaney

Reputation: 77347

csv.reader removes the quotes properly. You may be viewing a quoted string representation of the text instead of the actual text.

>>> new_data = '''"Apple Inc.",113.08,113.07
... "Alphabet Inc.",777.61,777.30
... "Microsoft Corporation",57.730,57.720'''
>>> 
>>> import csv
>>> 
>>> for row in csv.reader(new_data.splitlines()):
...     print(','.join(row))
... 
Apple Inc.,113.08,113.07
Alphabet Inc.,777.61,777.30
Microsoft Corporation,57.730,57.720
>>> 

Upvotes: 2

Related Questions