用python/django打印阿拉伯语字符

用python/django打印阿拉伯语字符,python,django,arabic,Python,Django,Arabic,我有一个脚本,可以修改django应用程序中的数据。我有一个excel文件中的数据,我会处理这些数据,然后用它更新我的模型,其中一些数据是阿拉伯语的,当我执行脚本时,会出现以下错误: Traceback (most recent call last): File "script.py", line 77, in <module> update_locations(path) File "script.py", line 36, in update_locatio

我有一个脚本,可以修改django应用程序中的数据。我有一个excel文件中的数据,我会处理这些数据,然后用它更新我的模型,其中一些数据是阿拉伯语的,当我执行脚本时,会出现以下错误:

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.

我需要关于打印这些阿拉伯字符的最佳方式的建议。提前谢谢。

这与阿拉伯字符无关。正如错误所说,您需要在那里分配Location的实例,而不是字符串。

Location是Location模型的外键,因此您不能只为其分配字符串。字符串是阿拉伯语与错误无关。谢谢,伙计们,问题出在哪里了,谢谢。
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")