我在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 是使用 pyxform (https://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.
有人可以在这里给我一些想法吗?