Reputation: 2671
Using python 2.7.4 in Ubuntu 13.10 I'm having my head done in by string encodings.
I have a dict, d, of parts. This includes some unicode strings, eg:
[{'part_number': u'70-0018', 'name': 'Baseball caps', 'slug': u'70-0018', 'link': 'https://URL/index.php?path=info&part_uuid=13840f24-3819-11e2-9e3b-002564adbfd8'},
{'part_number': u'80-0002', 'name': 'Top Hats', 'slug': u'80-0002', 'link': 'https://URL/index.php?path=info&part_uuid=9ff5627e-3827-11e2-9e3b-002564adbfd8'}]
I would like to test for duplicate slugs as I'm creating the dict, and on a clash add a revision number to retain uniqueness:
for row in from_cur.fetchall():
n = row[0]
pt = row[1]
pn = row[2]
u = "https://URL/index.php?path=info&part_uuid="+row[3]
new_pn = u"%s-%s" %(pt,pn)
sl = slugify(new_pn)
if sl in parts:
for i in range(10):
new_sl = sl+"-%s" % str(i)
if new_sl in parts:
pass
else:
sl = new_sl
break
part = {"name":"%s" % n,"part_number":"%s" % new_pn, "link":"%s" % u, "slug":"%s" % sl}
parts.append(part)
I have tried the "if sl in parts:" line with:
sl, unicode(sl), sl.encode('utf-8'), str(sl), sl.decode('ascii'), sl.decode('unicode-escape'), sl.decode('utf-8')
but none of them get me inside the if loop that changes the slug field.
How do I compare (or search for) a string with a unicode in a dict?
Upvotes: 0
Views: 115
Reputation: 37319
You're not showing where parts
is initially declared, but since you append dicts to it later I'm assuming it's a list of dicts. Possibly empty when you start. That kind of list membership test will not work. It's not a Unicode problem, it's a problem that you're testing to see if a string is in a list of dicts.
The best solution would be to maintain a set of used slugs as you go:
used_slugs = set()
for row in from_cur.fetchall():
...
if sl in used_slugs:
# do something to make a new slug
...
used_slugs.add(sl)
parts.append(part)
Upvotes: 1