0

with following python script:

#!/usr/bin/python3

import rethinkdb as r
import os
import configparser as c
from xml.dom import minidom
import urllib3

config = c.ConfigParser()
config

config.read("etc/db.py")

conn = r.connect(config.get("DB", "host"), config.get("DB", "port"), config.get("DB", "db")).repl()
http = urllib3.PoolManager()
get = http.request ("GET", 'http://s1-it.ogame.gameforge.com/api/universes.xml')

xmldoc = minidom.parseString(get.data)
itemlist = xmldoc.getElementsByTagName("universe")
id = []
href = []
a = 1
for s in itemlist:
    if a < len(itemlist):
        id.append("{ \"id\": \"" + s.attributes['id'].value + "\", \"href\": \"" + s.attributes['href'].value + "\" }, ")
    else:
        id.append("{ \"id\": \"" + s.attributes['id'].value + "\", \"href\": \"" + s.attributes['href'].value + "\" }")
    a = a + 1

data = "".join(id)

print(r.table("universes").insert([ data ]).run())

cursor = r.table("universes").run()
for document in cursor:
    print(document)

I have tried to parse a XML document and insert it into rethinkdb.

When I have launched python3 file.py I have seen following error:

Expected type OBJECT but found STRING

What is wrong in my script?

Thanks.

1 Answers1

0

You're trying to insert a string as a document when RethinkDB expect you to try to insert an object (dictionary in Python) or an array of objects.

You can solve this by just parsing the string into JSON.

import json

data = json.loads("".join(id))

print(r.table("universes").insert(data).run())
Jorge Silva
  • 4,424
  • 1
  • 18
  • 36