Reputation: 127
So I am working with django and an external List of names and values. With a custom template tag I am trying to display some values in my html template.
Here is a example of what the list could look like:
names.txt
hello 4343.5
bye 43233.4
Hëllo 554.3
whatever 4343.8
My template tag looks like this (simplified names of variables):
# -*- coding: utf-8 -*-
from django import template
register = template.Library()
@register.filter(name='operation_name')
def operation_name(member):
with open('/pathtofile/member.txt','r') as f:
for line in f:
if member.member_name in line:
number = float(line.split()[1])
if number is not member.member_number:
member.member_number = number
member.save()
return member.member_number
return 'Not in List'
It works fine for entries without specials char. But it stops when a name in member.member_names has special chars. So if member.member_names would be Hëllo the entire script just stops. I can't return anything. This is driving me crazy. Even the names without special chars won't be displayed after any name with special chars occurred.
I appreciate any help, thanks in advance.
EDIT:
So this did the trick:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
But I don't know if this is a good solution.
Upvotes: 3
Views: 185
Reputation: 4933
This may help you try to compare both to unicode:-
if (member.member_name).decode('latin1') in (line).decode('latin1'):
number = float(line.split()[1])
if number is not member.member_number:
member.member_number = number
member.save()
Upvotes: 1