Python:pyxform将csv转换为xls

2024-07-03 07:12:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Django有一个用来存储调查数据的系统。一些调查数据丢失了,现在我需要恢复这些数据。数据以csv和xls格式备份。你知道吗

下面这样的模型以不同的格式保存数据。你知道吗

class Forms(models.Model):
    xform = models.ForeignKey(XForm, related_name="fshistory")
    date = models.DateTimeField(auto_now=True)
    xls = models.FileField(upload_to=upload_to, null=True)
    json = models.TextField(default=u'')
    description = models.TextField(default=u'', null=True)
    xml = models.TextField()
    id_string = models.CharField(editable=False, max_length=255)
    title = models.CharField(editable=False, max_length=255)
    uuid = models.CharField(max_length=32, default=u'')
    version = models.CharField(max_length=255, default=u'')

存储在上述模型中的xml和json是使用pyxformhttps://github.com/XLSForm/pyxform)生成的。你知道吗

from pyxform.builder import create_survey_from_xls

survey = create_survey_from_xls(xls_file)
xml = survey.to_xml()

问题是我只能使用pyxform将xls文件转换成xml。其他以csv格式存储的数据不能直接转换成xml,所以我需要先将它们转换成xls。你知道吗

所以,我需要知道pyxform是否有任何模块可以将csv转换为xls,或者是否有其他方法可以这样做并获得正确的xml文件?你知道吗

我尝试了其他技术将csv转换成xls,比如:

    for filename in os.listdir(xls_directory):
        if os.path.isfile(os.path.join(xls_directory,filename)):
            if filename.endswith(".csv"):
                wb = openpyxl.Workbook()
                ws = wb.active
                xls_file = open(os.path.join(xls_directory, filename))

                reader = csv.reader(xls_file, delimiter=str(u';').encode('utf-8'))
                for row in reader:
                    ws.append(row)

                wb.save(''+filename.replace('.csv', '.xls'))
                print('File saved')

如果我在根据上述代码创建的xls文件中使用survey = create_survey_from_xls(xls_file),它会抛出一个错误:

Traceback (most recent call last):
  File "manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "/home/sanip/.virtualenvs/kobocat/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/sanip/.virtualenvs/kobocat/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/sanip/.virtualenvs/kobocat/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/sanip/.virtualenvs/kobocat/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/sanip/naxa/source/fieldsight/onadata/apps/fsforms/management/commands/create_xform_history.py", line 82, in handle
    survey = create_survey_from_xls(xls_file)
  File "/home/sanip/.virtualenvs/kobocat/lib/python2.7/site-packages/pyxform/builder.py", line 295, in create_survey_from_xls
    excel_reader = SurveyReader(path_or_file)
  File "/home/sanip/.virtualenvs/kobocat/lib/python2.7/site-packages/pyxform/xls2json.py", line 1176, in __init__
    path, warnings=self._warnings, file_object=self._file_object)
  File "/home/sanip/.virtualenvs/kobocat/lib/python2.7/site-packages/pyxform/xls2json.py", line 1090, in parse_file_to_json
    workbook_dict, default_name, default_language, warnings)
  File "/home/sanip/.virtualenvs/kobocat/lib/python2.7/site-packages/pyxform/xls2json.py", line 327, in workbook_to_json
    u"The survey sheet is either empty or missing important "
pyxform.errors.PyXFormError: The survey sheet is either empty or missing important column headers.

有人能给我一些建议吗?你知道吗


Tags: infrompyhomemodelsliblinevirtualenvs