Hello Djangonauts,

I am tryng to import non  ascii data stored in a file to do so I am
using a regular expression to parse the lines of the files in order
separates the "department number" from the department names. This is
working fine my problem comes when I am trying to use the Django's DB
API in order to save the data in a model that is described below.
The problems happens when matches[1] contains non ascii characters like
éàçàè...
r=Department(department=matches[1],department_number=matches[0],country="France")
r.save()
 I guess that I should add something to my script called
loader_departments.py  in order to support this.
It would be great if someone could explain what to me
Thank you for your help.


=========
models
=========
class Department(meta.Model):
    department = meta.CharField(maxlength=30)
    department_number = meta.IntegerField()
    country = meta.CharField(maxlength=30)
    def __repr__(self):
        return self.department + " "+str(self.department_number)
    class META:
        admin = meta.Admin(
            list_display =
('department','department_number','country'),
        )


the file where my data are stored look like this:
==========
departments.txt
============
89 Yonne

90 Territoire de Belfort

91 Essone

92 Hauts de Seine

93 Seine Saint-Denis

94 Val de Marne

95 Val d'Oise

971 Guadeloupe

972 Martinique

973 Guyane

974 Réunion

=============
loader_departments.py
==============

import os,codecs
from django.models.announceManager import *

os.chdir(os.path.abspath("E:\\instal\\django\\view_servicealapersonne\\votreservice\\_initialLoad"))
f = codecs.open("departments.txt",encoding='utf-8')

import re


regexobj = re.compile("([0-9]+)\s+([\w\s?]+)",re.UNICODE)

for l in f.xreadlines():
    print "the current line is :"+l
    try:
        matches = regexobj.search(l).groups()
        print "matches " +str(matches)

r=Department(department=matches[1],department_number=matches[0],country="France")
        r.save()
        print "ok"
    except:
        print "ko"
    
f.close()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to