jmc jmc - 1 year ago 87
Python Question

Mongo to Postgres connector error

I am trying to extract data from MongoDB and write back it to Postgres. The following script works fine but string inserting as individual character which contains multiple entry.

For example: Instead of


'Apple' its inserting 'A', 'P', 'P' ,'L' ,'E'


for line in results:
name = line['name']
cur.execute("""INSERT INTO xyz(Column1) VALUES('%s')""" % \
(line['name']))

Answer Source

if name is a list ['A', 'P', 'P', 'L', 'E'] then you can combine it into a single string with the string method join:

name = ''.join(list['name'])
cur.execute("""INSERT INTO xyz(Column1) VALUES('%s')""" %name)

Note that your original code doesn't use the name variable you've created.

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