本文整理汇总了Python中tablib.Dataset方法的典型用法代码示例。如果您正苦于以下问题:Python tablib.Dataset方法的具体用法?Python tablib.Dataset怎么用?Python tablib.Dataset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tablib
的用法示例。
在下文中一共展示了tablib.Dataset方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_split_amounts
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def test_split_amounts(self):
dataset = tablib.Dataset(
["15/6/2016", "", "100.56", "Example payment"],
["16/6/2016", "60.31", "", "Example income"],
["17/6/2016", "", "-102.56", "Example payment 2"],
headers=["date", "amount_in", "amount_out", "description"],
)
self.makeResource().import_data(dataset)
self.assertEqual(StatementLine.objects.count(), 3)
obj = StatementLine.objects.all().order_by("date")
self.assertEqual(obj[0].date, date(2016, 6, 15))
self.assertEqual(obj[0].amount, Decimal("-100.56"))
self.assertEqual(obj[0].description, "Example payment")
self.assertEqual(obj[1].date, date(2016, 6, 16))
self.assertEqual(obj[1].amount, Decimal("60.31"))
self.assertEqual(obj[1].description, "Example income")
self.assertEqual(obj[2].date, date(2016, 6, 17))
self.assertEqual(obj[2].amount, Decimal("-102.56"))
self.assertEqual(obj[2].description, "Example payment 2")
示例2: process
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def process(self, bom_file):
""" Process a BOM file """
self.data = None
ext = os.path.splitext(bom_file.name)[-1].lower()
if ext in ['.csv', '.tsv', ]:
# These file formats need string decoding
raw_data = bom_file.read().decode('utf-8')
elif ext in ['.xls', '.xlsx']:
raw_data = bom_file.read()
else:
raise ValidationError({'bom_file': _('Unsupported file format: {f}'.format(f=ext))})
try:
self.data = tablib.Dataset().load(raw_data)
except tablib.UnsupportedFormat:
raise ValidationError({'bom_file': _('Error reading BOM file (invalid data)')})
except tablib.core.InvalidDimensions:
raise ValidationError({'bom_file': _('Error reading BOM file (incorrect row size)')})
示例3: create_dataset
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def create_dataset(self, in_stream):
"""
Create dataset from first sheet.
"""
from io import BytesIO
import openpyxl
xlsx_book = openpyxl.load_workbook(BytesIO(in_stream), read_only=True)
dataset = tablib.Dataset()
sheet = xlsx_book.active
# obtain generator
rows = sheet.rows
dataset.headers = [cell.value for cell in next(rows)]
for row in rows:
row_values = [cell.value for cell in row]
dataset.append(row_values)
return dataset
#: These are the default formats for import and export. Whether they can be
#: used or not is depending on their implementation in the tablib library.
示例4: dataset
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def dataset(self):
"""A Tablib Dataset representation of the RecordCollection."""
# Create a new Tablib Dataset.
data = tablib.Dataset()
# If the RecordCollection is empty, just return the empty set
# Check number of rows by typecasting to list
if len(list(self)) == 0:
return data
# Set the column names as headers on Tablib Dataset.
first = self[0]
data.headers = first.keys()
for row in self.all():
row = _reduce_datetimes(row.values())
data.append(row)
return data
示例5: clean_csv_headers
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def clean_csv_headers(csv):
"""
Remove commas, line breaks, etc, anything that will screw
up the translation from CSV -> database table. CSVKit, in particular,
doesn't like header columns with these chars in it.
"""
data = Dataset().load(csv, format="csv")
headers = [re.sub("[,\"'\n]", "", h) for h in data.headers]
new_data = Dataset(headers=headers)
for row in data:
new_data.append(row)
return new_data.export("csv")
# NOTE: InvalidDimensions
示例6: get_csv_from_url
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def get_csv_from_url(self, sheet_url):
"""
Return a CSV (text data) from a protected Google sheet URL.
"""
sheet_id = extract_key_from_csv_url(sheet_url)
values = self.get_sheet_values(sheet_id)
headers = [re.sub("[:,\"'\n]", "", h) for h in values.pop(0)]
logger.error("Sheet Headers: %s" % headers)
# TODO: this should be shared across screendoor importer
data = Dataset(headers=headers)
n_headers = len(headers)
for row in values:
n_cols = len(row)
if n_cols < n_headers:
row += [""] * (n_headers - n_cols)
data.append(row)
csv_data = data.export("csv")
return csv_data
示例7: test_can_build_csv_with_ids
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def test_can_build_csv_with_ids(self, mockget):
importer = ScreendoorImporter(api_key="KEY", base_url="https://fake.tld")
csv = importer.build_csv_from_data(LIST_FORMS[0], LIST_RESPONSES)
self.assertTrue(csv)
parsed_csv = Dataset().load(csv)
self.assertTrue("id" in parsed_csv.headers)
# @patch.object(requests, "get")
# def test_can_use_buildin_id_during_import(self, mockget):
# mockresponse = Mock()
# mockget.return_value = mockresponse
# mockresponse.json.side_effect = [
# LIST_FORMS, LIST_RESPONSES
# ]
# importer = ScreendoorImporter(api_key="KEY", base_url="https://fake.tld")
# csv = importer.build_csv(6076)
# print("Screendoor CSV", csv)
# self.assertTrue(len(csv))
# self.assertTrue(False)
示例8: report
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def report(self) -> t.Optional[str]:
"""
Create an report and output it as configured.
:return: the report string if ``to_string == True``
"""
if not self.misc["out"] == "-" and not os.path.exists(os.path.dirname(self.misc["out"])):
logging.error("Folder for report ({}) doesn't exist".format(os.path.dirname(self.misc["out"])))
exit(1)
with click.open_file(self.misc["out"], mode='w') as f:
import tablib
data = tablib.Dataset(itertools.chain.from_iterable(x.split(",") for x in self.misc["columns"]))
for row in self._table():
data.append(row)
f.write(data.csv)
chown(f)
示例9: load
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def load(app_id):
"""Load the data from wherever it is found."""
path_to_data = find_experiment_export(app_id)
if path_to_data is None:
raise IOError("Dataset {} could not be found.".format(app_id))
return Data(path_to_data)
示例10: __init__
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def __init__(self, path):
self.odo_resource = odo.resource(path)
self.tablib_dataset = tablib.Dataset().load(open(path).read(), "csv")
示例11: get_dataset
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def get_dataset(self):
reader = self._get_csv_reader()
if self.has_headings:
six.next(reader)
data = list(reader)
headers = [
column.to_field or "col_%s" % column.column_number for column in self.columns.all()
]
return Dataset(*data, headers=headers)
示例12: test_import_one
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def test_import_one(self):
dataset = tablib.Dataset(
["15/6/2016", "5.10", "Example payment"], headers=["date", "amount", "description"]
)
self.makeResource().import_data(dataset)
self.assertEqual(StatementLine.objects.count(), 1)
obj = StatementLine.objects.get()
self.assertEqual(obj.date, date(2016, 6, 15))
self.assertEqual(obj.amount, Decimal("5.10"))
self.assertEqual(obj.description, "Example payment")
示例13: test_import_skip_duplicates
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def test_import_skip_duplicates(self):
dataset = tablib.Dataset(
["15/6/2016", "5.10", "Example payment"], headers=["date", "amount", "description"]
)
self.makeResource().import_data(dataset)
# Now do the import again
self.makeResource().import_data(dataset)
# The record in the second should have been ignored
self.assertEqual(StatementLine.objects.count(), 1)
示例14: test_import_skip_duplicates_whitespace
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def test_import_skip_duplicates_whitespace(self):
dataset1 = tablib.Dataset(
["15/6/2016", "5.10", "Example payment"], headers=["date", "amount", "description"]
)
dataset2 = tablib.Dataset(
["15/6/2016", "5.10", "Example payment "], # Whitespace added
headers=["date", "amount", "description"],
)
self.makeResource().import_data(dataset1)
self.makeResource().import_data(dataset2)
# The record in the second should have been ignored
self.assertEqual(StatementLine.objects.count(), 1)
示例15: test_import_two_identical
# 需要导入模块: import tablib [as 别名]
# 或者: from tablib import Dataset [as 别名]
def test_import_two_identical(self):
"""Ensure they both get imported and that one doesnt get skipped as a duplicate
After all, if there are two imported rows that look identical, it is probably because
there are two identical transactions.
"""
dataset = tablib.Dataset(
["15/6/2016", "5.10", "Example payment"],
["15/6/2016", "5.10", "Example payment"],
headers=["date", "amount", "description"],
)
self.makeResource().import_data(dataset)
self.assertEqual(StatementLine.objects.count(), 2)