error: list indices must be indtegers, not str
<pre><code>fields = ["date", "time", "a_x", "a_y", "a_z", "roll", "pitch", "yaw", "ug_x", "ug_y", "ug_z", "o2", "hyd", "bpm"]

import csv

f = open("data.TXT")


dialect = csv.Sniffer().sniff(f.read(), delimiters=' ')


f.seek(0)

reader = csv.reader(f, dialect)

rows = []

obj = {}

for row in reader:
  for i, field in enumerate(fields):
    obj[field] = row[i]
    if field == "a_x":
        obj[field] = float(row[i])
    elif field == "a_y":
        obj[field] = float(row[i])
    elif field == "a_z":
        obj[field] = float(row[i])
    elif field == "roll":
        obj[field] = float(row[i])
    elif field == "pitch":
        obj[field] = float(row[i])
    elif field == "yaw":
        obj[field] = float(row[i])
    elif field == "ug_x":
        obj[field] = float(row[i])
    elif field == "ug_y":
        obj[field] = float(row[i])
    elif field == "ug_z":
        obj[field] = float(row[i])
    elif field == "o2":
        obj[field] = int(row[i])
    elif field == "hyd":
        obj[field] = int(row[i])
    elif field == "bpm":
        obj[field] = int(row[i])
    else:
        obj[field] = str(row[i])
  rows.append(obj)


import requests
requests.get("http://localhost:5000/api/players")


for obj in rows:
    # *** Here is the error ***
    row["player_id"] = ['03fd6907-64fc-46e7-b1f2-38af96c48037']
    requests.post("http://localhost:5000/api/impact")
</code></pre>

<p>The error occurs in the second to last line of code, as indicated by a comment. The code is intended to upload information from a text file to a database, changing the data types match a schema written in a separate file.</p>

<p>I am not sure of the source of the error in line 56, that the list indices must be integers, not str, or how to fix it.</p>

<pre><code>for obj in rows:
    row["player_id"] = ['03fd6907-64fc-46e7-b1f2-38af96c48037']
</code></pre>

<p>The loop variable is called <code>obj</code> but you are trying to access <code>row</code>.  <code>row</code> is a list left over from a previous loop (<code>for row in reader:</code>).  Change <code>row</code> to <code>obj</code> and the problem should go away:</p>

<pre><code>for obj in rows:
    obj["player_id"] = ['03fd6907-64fc-46e7-b1f2-38af96c48037']
</code></pre>

<p>Also, it is a bug to define <code>obj = {}</code> outside of the <code>for row in reader</code> loop.  You will keep mutating the same dictionary and all <code>obj</code> in <code>rows</code> will be identical because they will all be references to the same object. </p>

<p>I would rewrite that loop as:</p>

<pre><code>float_fields = {'a_x', 'a_y', 'a_z', 'roll', 'pitch', 'yaw', 'ug_x', 'ug_y', 'ug_z'}
int_fields = {'o2', 'hyd', 'bpm'}
for row in reader:
    obj = {} # moved inside loop
    for item, fieldname in zip(row, fields):
        if fieldname in float_fields:
            obj[fieldname] = float(item)
        elif fieldname in int_fields:
            obj[fieldname] = int(item)
        else:
            obj[fieldname] = str(item)  
    rows.append(obj)
</code></pre>

python database mongodb csv