我试图通过将包含数据的CSV文件转换为json文件,并使用loaddata将其馈送到Django中,来将fixture加载到我的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。或者说我确实这样做了吗?如果是这样,那么是什么导致了递归问题呢?
预计到达时间:以下是完整的错误消息:
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),但也没有起作用。有人可以帮忙吗?
for line in f
足以进行顺序访问。) - 9000