TypeError: string indices must be integers, why?
<p>I get</p>
<blockquote>
<p>TypeError: string indices must be integers</p>
</blockquote>
<p>with this code:</p>
<pre><code>from xml.etree import cElementTree as ET
from collections import defaultdict
def etree_to_dict(t):
d = {t.tag: {} if t.attrib else None}
children = list(t)
if children:
  dd = defaultdict(list)
  for dc in map(etree_to_dict, children):
    for k, v in dc.items():
      dd[k].append(v)
      d = {t.tag: {k: v[0] if len(v) == 1 else v for k, v in dd.items()}}
      if t.attrib:
        d[t.tag].update(('@' + k, v) for k, v in t.attrib.items())
      if t.text:
        text = t.text.strip()
        if children or t.attrib:
          if text:
            d[t.tag]['#text'] = text
          else:
            d[t.tag] = text
          return d
</code></pre>
<p>I think the problem is here:</p>
<pre><code>e = ET.parse('adversolutions.xml')
d = etree_to_dict(e.getroot())
products = d['yml_catalog']['shop5']

for p in products:
  product = Product()
  product.name = p['name']
  product.description = p['description']
  product.price = p['price']
  product.category = p['category']
  product.external_url = p['url']
  product.category_id = p['categoryId']
  product.currencies = p['currencies']

  product.save()
</code></pre>
<p>How can I fix this bug?</p>
<p>All this is to deserialize XML and save the data and model in Django.</p>
<p>All of the traceback:</p>
<pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
  File &quot;manage.py&quot;, line 10, in &lt;module&gt;
    execute_from_command_line(sys.argv)
  File &quot;/home/alexandr/beo/lib/python3.4/site-packages/django/core/management/__init__.py&quot;, line 385, in execute_from_command_line
    utility.execute()
  File &quot;/home/alexandr/beo/lib/python3.4/site-packages/django/core/management/__init__.py&quot;, line 354, in execute
    django.setup()
  File &quot;/home/alexandr/beo/lib/python3.4/site-packages/django/__init__.py&quot;, line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File &quot;/home/alexandr/beo/lib/python3.4/site-packages/django/apps/registry.py&quot;, line 108, in populate
    app_config.import_models(all_models)
  File &quot;/home/alexandr/beo/lib/python3.4/site-packages/django/apps/config.py&quot;, line 202, in import_models
    self.models_module = import_module(models_module_name)
  File &quot;/home/alexandr/beo/lib/python3.4/importlib/__init__.py&quot;, line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 2254, in _gcd_import
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 2237, in _find_and_load
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 2226, in _find_and_load_unlocked
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1200, in _load_unlocked
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1129, in _exec
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1471, in exec_module
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 321, in _call_with_frames_removed
  File &quot;/home/alexandr/beo/posts/models.py&quot;, line 9, in &lt;module&gt;
    from products.models import Product
  File &quot;/home/alexandr/beo/products/models.py&quot;, line 155, in &lt;module&gt;
    product.name = p['name']
TypeError: string indices must be integers
</code></pre>
<p>With this line, all works:</p>
<pre><code>products = d['yml_catalog']['shop5']
</code></pre>
<p>The problems start here:</p>
<pre><code>for p in products:
  product = Product()
  product.name = p['name']
  product.description = p['description']
  product.price = p['price']
  product.category = p['category']
  product.external_url = p['url']
  product.category_id = p['categoryId']
  product.currencies = p['currencies']

  product.save()
</code></pre>
<p>print(products):</p>
<p>Piece of the large XML file:</p>
<pre><code>'@available': 'true', 'param': {'@name': 'Размер', '@unit': 'INT', '#text': 'L'}, '@id': '117213', 'name': 'Штаны Pocket Grey', 'oldprice': '1990', '@group_id': '1172', 'description': None, 'picture': 'http:///upload/iblock/559/559b4fe1c3b397bf84eb16686bd5aadf.jpg', 'url': 'http:///catalog/shtany/galife_pocket_grey/', 'currencyId': 'RUB', 'price': '1990'}, {'vendor': 'Envy Lab', '@available': 'true', 'param': {'@name': 'Размер', '@unit': 'INT', '#text': 'S'}, '@id': '117511', 'name': 'Галифе Dark Black', 'oldprice': '1990', '@group_id': '1175', 'description': None, 'picture': 'http:///upload/iblock/11d/11d1c223f4e65b347cd5823fff9e5e9b.jpg', 'url': 'http:///catalog/shtany/galife_dark_black/', 'currencyId': 'RUB', 'price': '1990'}, {'vendor': 'Envy Lab', '@available': 'true', 'param': {'@name': 'Размер', '@unit': 'INT', '#text': 'M'}, '@id': '117512', 'name': 'Галифе Dark Black', 'oldprice': '1990', '@group_id': '1175', 'description': None, 'picture': 'http:///upload/iblock/11d/11d1c223f4e65b347cd5823fff9e5e9b.jpg', 'url': 'http:///catalog/shtany/galife_dark_black/', 'currencyId': 'RUB', 'price': '1990'}, {'vendor': 'Envy Lab', '@available': 'true', 'param': {'@name': 'Размер', '@unit': 'INT', '#text': 'L'}, '@id': '117513', 'name': 'Галифе Dark Black', 'oldprice': '1990', '@group_id': '1175', 'description': None, 'picture': 'http:///upload/iblock/11d/11d1c223f4e65b347cd5823fff9e5e9b.jpg', 'url': 'http:///catalog/shtany/galife_dark_black/', 'currencyId': 'RUB', 'price': '1990'}]}, 'url': 'http://www.envylab.ru', 'name': 'Envylab', 'categories': {'category': [{'#text': 'Поло', '@id': '61'}, {'#text': 'Толстовки', '@id': '65'}, {'#text': 'Штаны', '@id': '72'}, {'#text': 'Шапки', '@id': '74'}, {'#text': 'Рубашки', '@id': '76'}]}, 'company': 'Envylab'}
</code></pre>
<p>print(p):</p>
<p>One of the results:</p>
<pre><code>categories
</code></pre>
<p>print(type(products))</p>
<pre><code>&lt;class 'dict'&gt;
</code></pre>