本文整理匯總了Python中core.db.connection.DataHubConnection.import_file方法的典型用法代碼示例。如果您正苦於以下問題:Python DataHubConnection.import_file方法的具體用法?Python DataHubConnection.import_file怎麽用?Python DataHubConnection.import_file使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類core.db.connection.DataHubConnection
的用法示例。
在下文中一共展示了DataHubConnection.import_file方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: import_file
# 需要導入模塊: from core.db.connection import DataHubConnection [as 別名]
# 或者: from core.db.connection.DataHubConnection import import_file [as 別名]
def import_file(repo_base, table_name, file_path, file_format='CSV',
delimiter=',', header=True, encoding='ISO-8859-1', quote_character='"'):
superuser_con = DataHubConnection(
user=settings.DATABASES['default']['USER'],
password=settings.DATABASES['default']['USER'],
repo_base=repo_base)
return superuser_con.import_file(
table_name=table_name,
file_path=file_path,
file_format=file_format,
delimiter=delimiter,
header=header,
encoding=encoding,
quote_character=quote_character)
示例2: import_file
# 需要導入模塊: from core.db.connection import DataHubConnection [as 別名]
# 或者: from core.db.connection.DataHubConnection import import_file [as 別名]
def import_file(username, repo_base, repo, table, file_name,
file_format='CSV', delimiter=',', header=True,
encoding='ISO-8859-1', quote_character='"'):
# check for permissions
delimiter = delimiter.decode('string_escape')
res = DataHubManager.has_repo_privilege(
username, repo_base, repo, 'CREATE')
if not res:
raise PermissionDenied(
'Access denied. Missing required privileges.')
# prepare some variables
file_path = user_data_path(repo_base, repo, file_name)
table_name, _ = os.path.splitext(file_name)
table_name = clean_str(table_name, 'table')
dh_table_name = '%s.%s.%s' % (repo_base, repo, table_name)
# open the file
f = codecs.open(file_path, 'r', 'ISO-8859-1')
data = csv.reader(f, delimiter=delimiter)
# create a table for the data
cells = data.next()
columns = [clean_str(str(i), 'col') for i in range(0, len(cells))]
if header:
columns = map(lambda x: clean_str(x, 'col'), cells)
columns = rename_duplicates(columns)
query = 'CREATE TABLE %s (%s text' % (dh_table_name, columns[0])
for column in columns[1:len(columns)]:
query += ', %s %s' % (column, 'text')
query += ')'
manager = DataHubManager(user=username, repo_base=repo_base)
manager.execute_sql(query=query)
# populate the newly created table with data from the csv
superuser_con = DataHubConnection(
user=settings.DATABASES['default']['USER'],
password=settings.DATABASES['default']['USER'],
repo_base=repo_base)
return superuser_con.import_file(
table_name=dh_table_name,
file_path=file_path,
file_format=file_format,
delimiter=delimiter,
header=header,
encoding=encoding,
quote_character=quote_character)
示例3: import_file
# 需要導入模塊: from core.db.connection import DataHubConnection [as 別名]
# 或者: from core.db.connection.DataHubConnection import import_file [as 別名]
def import_file(
repo_base,
table_name,
file_path,
file_format="CSV",
delimiter=",",
header=True,
encoding="ISO-8859-1",
quote_character='"',
):
superuser_con = DataHubConnection(
user=settings.DATABASES["default"]["USER"],
password=settings.DATABASES["default"]["USER"],
repo_base=repo_base,
)
return superuser_con.import_file(
table_name=table_name,
file_path=file_path,
file_format=file_format,
delimiter=delimiter,
header=header,
encoding=encoding,
quote_character=quote_character,
)