UNC路径的Python和MySQL转义序列

时间:2018-07-03 21:50:08

标签: python mysql python-3.x unc

我花了一点时间来获取加载数据本地文件以使用python。我在Python 3.6.5中尝试了多种方法:

sqlpath = '\\corp.domain.com\dept\DCGSI\Extracts\SIM\'
sqlpath = "\\corp.domain.com\dept\DCGSI\Extracts\SIM\"
sqlpath = '\\\\corp.domain.com\\dept\\DCGSI\\Extracts\\SIM\\'
sqlpath = '//corp.domain.com/dept/DCGSI/Extracts/SIM/'
sqlpath = os.path.join("\\corp.domain.com","dept","DCGSI","Extracts","SIM")
sqlpath = os.path("\\\\corp.domain.com\dept\DCGSI\Extracts\SIM\")

为了节省理智,我似乎无法在下面使用转义符。我在这里至少阅读了10个不同的帖子,并尝试了所有这些建议。我在做什么错,因此可以在loadQuery中使用unc路径。

这是(当前)整个脚本:

import config
import os
import pymysql


username = config.username
dbpassword = config.dbpassword
dbhost = config.dburl
conn =  pymysql.connect(host=dbhost, port=3306,user=username,password=dbpassword,db='dcgsreports',autocommit=True,local_infile=1)
path = '//corp.domain.com/dept/DCGSI/Extracts/SIM'
tables = []
files = []

fileNames = os.listdir(path)
for fileNames in fileNames:
    if fileNames.endswith(".csv"):
        files.append(fileNames)
        tables.append(fileNames.split('.')[0])

for f,t in zip(files, tables) :
    truncQuery = '''TRUNCATE TABLE %s''' %t
    loadQuery = '''LOAD DATA LOCAL INFILE '//corp.domain.com/dept/DCGSI/Extracts/SIM/%s' INTO TABLE %s FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\\r\\n' IGNORE 1 LINES;''' %(f, t)
    print(loadQuery)
    cursor = conn.cursor()
    cursor.execute(truncQuery)
    cursor.execute(loadQuery)
    conn.commit()
conn.close()

该打印语句显示以下内容应作为查询传递(应该正确): 加载数据本地文件'//corp.domain.com/dept/DCGSI/Extracts/SIM/SIM_Availability.csv'到表SIM_Availability字段中,以','可选,并以','可选,并以',' '忽略1行;

所有表都是空的,尽管这似乎表明只有truncQuery正在执行。

1 个答案:

答案 0 :(得分:0)

考虑Python的操作系统不可知os.path.join(),不要担心反斜杠或正斜杠。并使用string.format甚至Python 3.6的新f-string(像Perl和Perl的 $ string 这样的文字字符串插值)。不建议使用用于字符串插值的模运算符。还要确保执行commit操作:

path = r"\\corp.domain.com\dept\DCGSI\Extracts\SIM"

for t, f in zip(tables, files) :

    truncQuery = "TRUNCATE TABLE `{t_name}`"
    loadQuery = """LOAD DATA LOCAL INFILE '{f_name}'
                   INTO TABLE `{t_name}` FIELDS TERMINATED BY ',' 
                   OPTIONALLY ENCLOSED BY '"' LINES 
                   TERMINATED BY '\r\n' IGNORE 1 LINES;"""

    print(loadQuery)

    cursor = conn.cursor()
    cursor.execute(truncQuery.format(t_name = t)
    cursor.execute(loadQuery.format(f_name = os.path.join(path, f),
                                    t_name = t))

    cursor.close()
    conn.commit()

带有f弦

for t, f in zip(tables, files) :

    f_name = os.path.join(path, f)

    truncQuery = f"TRUNCATE TABLE `{t}`"
    loadQuery = f"""LOAD DATA LOCAL INFILE '{f_name}'
                    INTO TABLE `{t}` FIELDS TERMINATED BY ',' 
                    OPTIONALLY ENCLOSED BY '"' LINES 
                    TERMINATED BY '\r\n' IGNORE 1 LINES;"""

    print(loadQuery)

    cursor = conn.cursor()
    cursor.execute(truncQuery)
    cursor.execute(loadQuery)

    cursor.close()
    conn.commit()