我花了相当多的时间研究使用 Teradata Fastload 上传 csv 文件的合理方法,但文档很短、有限且不清楚。
给定某个 csv,如何将其上传到给定的数据库?
我创建了一个带有 Python 类的Gist,其中包含使用 pyodbc 与 Teradata 通信的所有必要方法。
专门用于使用此方法上传 csv 文件,您的 csv 文件必须满足某些条件:
例子:
“值1” | "值2" | “值3”
“值1” | "值2" | “值3”
“值1” | "值2" | “值3”
这可以通过使用熊猫来实现:
import pandas as pd
from csv import QUOTE_ALL
data.to_csv('tmp.csv', index=False, sep='|', quotechar='"', quoting=QUOTE_ALL, header=False)
Run Code Online (Sandbox Code Playgroud)
在此之后,您可以使用此功能:
def upload_csv(database, csv_file, table, columns, user, password, verbose=True):
"""
This function uses Fastlaod utily to upload csv file delimited with "|" instead of ',' and where all values in
file are quoted. Ex: "value1" | "value2" | . . .
:param csv_file: csv file without columns names
:param table: Insertion table
:param columns: Column names
:param user: username
:param password:
:param verbose: True | False if output is required
"""
script_text = fastload_template.substitute(DATA_FILE=csv_file,
COLUMN_DEFINITIONS=',\n'.join(['"' + column + '" (varchar(2000))' for column in columns]),
VALUES=',\n'.join([':' + '"' + column + '"' for column in columns]),
DATABASE=database, TABLE=table, USER=user, PASSWORD=password)
tmp_file = csv_file[:-4]
script = open(tmp_file, "w")
script.writelines("%s\n" % script_text)
script.close()
try:
if verbose:
run(["fastload < " + tmp_file], check=True, shell=True)
else:
run(["fastload < " + tmp_file], check=True, shell=True, stdout=open(os.devnull, 'w'))
except CalledProcessError as e:
if e.args[0] != 8: # Fastload gives error 8 but insert is working.. so don't touch :)
raise e
os.remove(tmp_file)
Run Code Online (Sandbox Code Playgroud)