Edward Okech
Edward Okech

Reputation: 161

Printing Arabic characters in python/django

I have a script that modifies data in a django app.I have data in an excel file that i process then update my models with it, some of the data is in Arabic and when i execute the script i get the following error:

Traceback (most recent call last):

  File "script.py", line 77, in <module>

    update_locations(path)

  File "script.py", line 36, in update_locations

    household.location = new_location

  File "/data/envs/ve.maidea/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py", line 207, in __set__

    self.field.remote_field.model._meta.object_name,

ValueError: Cannot assign "'\xd8\xa7\xd9\x84\xd8\xa8\xd8\xad\xd9\x8a\xd8\xb1\xd9\x87'": "Household.location" must be a "Location" instance.

I think the error is been raised by these Arabic characters.

here is my script:

import django
django.setup()

import sys
reload(sys)    # to re-enable sys.setdefaultencoding()
sys.setdefaultencoding('utf-8')

import xlrd
from django.db import transaction 
from foodnet.apps.registration.models import Household
from geo.models import Location

log_file = "/opt/cv_instances/cv1/autodeploy/branches/nboreports/maidea/egypt/data_import_files/egypt_beheira_locations.txt"
logfile_to_write = open(log_file, "w")

def process_file(path):
    book = xlrd.open_workbook(path)
    print("Got {0} number of sheets.".format(book.nsheets))

    hh_counter = 0

    for sheet_num in range(book.nsheets-1, -1, -1):
        sheet = book.sheet_by_index(sheet_num)
        print("Processing sheet number {0} ({1})".format(sheet_num, sheet.name))
        for row_idx in range(1, sheet.nrows):
            with transaction.atomic():
                try:
                    household_name = str(sheet.row_values(row_idx)[0]).strip().replace(".0","")
                    # old_location = str(sheet.row_values(row_idx)[1]).strip().replace(".0","")
                    new_location = str(sheet.row_values(row_idx)[2]).strip().replace(".0","")

                    if household_name:
                        household = Household.objects.get(office__slug='eg-co',name=household_name)
                        # print(household.name, household.location)
                        #update new locations
                        household.location = new_location
                        household.save()
                        hh_counter += 1

                        logfile_to_write.write("Household {0} updated to location {1}".format(household, household.location))
                except Household.DoesNotExist:
                    continue
    print("Done looping and updating locations")
    print("================================================================================================================================")

def delete_old_locations(path):
    """
    Delete old locations no longer needed by the country office
    """
    book = xlrd.open_workbook(path)
    print("Got {0} number of sheets.".format(book.nsheets))

    location_counter = 0

    for sheet_num in range(book.nsheets-1, -1, -1):
        sheet = book.sheet_by_index(sheet_num)
        print("Processing sheet number {0} ({1})".format(sheet_num, sheet.name))
        for row_idx in range(1, sheet.nrows):
            with transaction.atomic():
                try:
                    old_location = str(sheet.row_values(row_idx)[1]).strip().replace(".0","")
                    if old_location:
                        location = Location.objects.get(country__name="Egypt", name=old_location)
                        # print(location.name, location.country)
                        location.delete()
                        location_counter += 1
                        logfile_to_write.write("Location {0} deleted ".format(location))
                except Location.DoesNotExist:
                    continue
    print("Done looping and deleting locations")
    print("================================================================================================================================")


#call the our process file method
if __name__=="__main__":
    path = "/opt/cv_instances/cv1/autodeploy/branches/nboreports/maidea/egypt/data_import_files/egypt-sf-beheira-enrolments.xlsx"
    process_file(path)
    delete_old_locations(path)
    print("Done processing file")

I kindly need advice on the best way of printing this arabic characters.Thanks in advance.

Upvotes: 1

Views: 407

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599866

This has nothing to do with Arabic characters. As the error says, you need to assign an instance of Location there, not a string.

Upvotes: 2

Related Questions