-2

I want to replace special characters from email in django. I have Google this issue and found stack question which is very helpful question.

But there are some issues with this. If i tried this

a='testemail@email.com'
    replace=['@','.']
    for i in replace:
        a=a.replace(i,'_')

or this

u_name=re.sub(r'[^a-zA-Z0-9]', '_', str(email))

and in template

{% for i in u_name%}
{{i}}<br>
{% endfor %}

it will return

t
e
s
t
e
m
a
i
l
_
e
m
a
i
l
_
c
o
m

but i want like this testemail_email_com so that i can store it in DB with the help of loop but it store only first characters.

So please tell me how i can do this.

Thanks

I am migrating some data from other DB and wanted to store it into django db. Let say i have data for 1o users , the emails of these users store into following array. Now i wanted to replace special characters from these email so that i can use them as username.

Edited :

email.append(row[i][1]) 
Community
  • 1
  • 1
user1746291
  • 323
  • 2
  • 5
  • 15

1 Answers1

1

If u_name is a single string, you can just use this:

{{u_name}}<br>

You're iterating through each character of the string - i.e.

>>> for i in "mystring":
    print i


m
y
s
t
r
i
n
g

As per your edit:

If emails is the name of your array, you need to replace each element, then print it:

>>> emails = ["me@apple.com", "you@test.com"]
>>> emails = [re.sub(r'[^a-zA-Z0-9]', '_', x) for x in emails]
>>> print emails
['me_apple_com', 'you_test_com']

Then use template:

{% for email in emails%}
{{email}}<br>
{% endfor %}
Alex L
  • 8,748
  • 5
  • 49
  • 75
  • No u_name is an array and i have to replace all email that are under email array – user1746291 Feb 08 '13 at 09:50
  • @user1746291 It's not in your example though - you're clearly iterating through each letter. Could you show some more code? Where does the array come from? – Alex L Feb 08 '13 at 09:55