3

我正在尝试通过将带有数据的 CSV 转换为 json 文件,然后使用 loaddata 将它们输入 Django 来将固定装置加载到我的 Django (1.4) 应用程序中。但我不断收到最大递归深度错误。我环顾四周,它似乎与 json 文件中的非 ASCII 字符有关。但是,我很确定我不会那样做?这是我的代码:

    def csv_to_json(self,csv,dest,model,keys,sub):
    #keys is a dict formatted x:f where x = index of value v;
    #don't include pk
    with open(csv) as f:
        l = f.readlines()
    right = len(l[0].split(","))
    out = []
    for x in xrange(1,len(l)):
        if sub:
            line = re.sub(", "," ",l[x])
        line = re.sub(r'(,[\'\"*.]+|[\'\"*.]+,)','',l[x])
        line = unicodedata.normalize('NFKD',unicode(line,'utf-8','ignore')).encode('ASCII','ignore')
        line.encode('ASCII')
        splt = line.split(",")
        print len(splt) 
        if len(splt) == right:
            for y in xrange(len(splt)):
                if re.match(r'\d{4}-\d{2}-\d{2}',str(splt[y])):
                    splt[y] = int(splt[y][:4])
                try:
                    if splt[y].isupper():
                        splt[y] = splt[y].title()
                    splt[y] = splt[y].rstrip()
                except AttributeError:
                    continue
                try:    
                    splt[y] = int(splt[y])
                except ValueError:
                    try:
                        splt[y] = float(splt[y])
                    except ValueError:
                        continue

            d = {}
            d["model"] = "dishes.%s"%(model)
            d["pk"] = splt[0]
            d["fields"] = {}
            for k,v in keys.items():
                print v,k
                try:
                    d["fields"][v] = splt[k]
                except IndexError:
                    continue
            out.append(d)
    with open(dest,"w") as f:
        json_out = simplejson.dump(out,f,separators=(",",":"))

如您所见,我正在将 CSV 的内容简要转换为 UTF-8,以便使用 unicodedata 清除它们的变音符号(我必须这样做,因为我在尝试加载时收到“无效的延续字符”消息它们与loaddata),但随后我将所有内容都转换回ASCII。还是我?如果我是,那么导致递归问题的原因是什么?

ETA:这是完整的错误消息:

Problem installing fixture
'/Users/samuelraker/django/menus/menus/dishes/fixtures/Classification.json':                
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 190, in handle
for obj in objects:
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/serializers/json.py", line 47, in Deserializer
raise DeserializationError(e)
DeserializationError: maximum recursion depth exceeded while calling a Python object

重新编辑以进一步添加:这是我尝试加载的 JSON 文件之一的片段。

[
{
    "pk":1,
    "model":"dishes.dish",
    "fields":{
        "name":"Consomme printaniere royal"
    }
},
{
    "pk":2,
    "model":"dishes.dish",
    "fields":{
        "name":"Chicken gumbo"
    }
},
{
    "pk":3,
    "model":"dishes.dish",
    "fields":{
        "name":"Tomato aux croutons"
    }
},
{
    "pk":4,
    "model":"dishes.dish",
    "fields":{
        "name":"Onion au gratin"
    }
},
{
    "pk":5,
    "model":"dishes.dish",
    "fields":{
        "name":"St. Emilion"
    }
},
{
    "pk":7,
    "model":"dishes.dish",
    "fields":{
        "name":"Radishes"
    }
},
{
    "pk":8,
    "model":"dishes.dish",
    "fields":{
        "name":"Chicken soup with rice"
    }
},
{
    "pk":9,
    "model":"dishes.dish",
    "fields":{
        "name":"Clam broth (cup)"
    }
},
{
    "pk":11,
    "model":"dishes.dish",
    "fields":{
        "name":"Clear green turtle"
    }
},
{
    "pk":13,
    "model":"dishes.dish",
    "fields":{
        "name":"Anchovies"
    }
},
{
    "pk":14,
    "model":"dishes.dish",
    "fields":{
        "name":"Fresh lobsters in every style"
    }
},
{
    "pk":15,
    "model":"dishes.dish",
    "fields":{
        "name":"Celery"
    }
},
{
    "pk":16,
    "model":"dishes.dish",
    "fields":{
        "name":"Pim-olas"
    }
},
{
    "pk":17,
    "model":"dishes.dish",
    "fields":{
        "name":"Caviar"
    }
},
{
    "pk":18,
    "model":"dishes.dish",
    "fields":{
        "name":"Sardines"
    }
},
{
    "pk":19,
    "model":"dishes.dish",
    "fields":{
        "name":"India chutney"
    }
},
{
    "pk":20,
    "model":"dishes.dish",
    "fields":{
        "name":"Pickles"
    }
},
{
    "pk":21,
    "model":"dishes.dish",
    "fields":{
        "name":"English walnuts"
    }
},
{
    "pk":22,
    "model":"dishes.dish",
    "fields":{
        "name":"Pate de foies-gras"
    }
},
{
    "pk":23,
    "model":"dishes.dish",
    "fields":{
        "name":"Pomard"
    }
},
{
    "pk":26,
    "model":"dishes.dish",
    "fields":{
        "name":"Clams"
    }
},
{
    "pk":27,
    "model":"dishes.dish",
    "fields":{
        "name":"Oysters"
    }
},
{
    "pk":28,
    "model":"dishes.dish",
    "fields":{
        "name":"Claremont planked shad"
    }
},
...

编辑3:节日快乐!我只是将其中一个 JSON 文件的一小段粘贴到一个单独的文件中,通过 JSONLint 运行它以确保,然后将其传递给 loaddata ......我得到了同样的错误消息。我的模型可能有问题吗?

编辑 4:所以我尝试将我的数据编码为 YAML 而不是 JSON,但它不起作用。伙计们,我真的很难过。有人请帮忙!

编辑5:我什至尝试将我的数据库后端更改为django-mysql-pymysql(http://pypi.python.org/pypi/django-mysql-pymysql/0.1),但这也不起作用。任何人?请?

4

1 回答 1

2

事实证明,问题在于我的模型中有一个名为“pk”的字段,其中“primary_key=True”。如果有一个 Django 的“保留字”列表会很好,因为我的错误似乎很容易犯,而且,如果不使用 shell 进行调试,很难捕捉到。我也遇到了以“_id”结尾的手动定义字段的问题,AFAIK 也没有记录。

于 2013-01-03T18:04:11.820 回答