在插入SQL Server时保留CR LF(换行)

时间:2017-10-18 01:05:32

标签: python python-2.7 pyodbc

我正在尝试将数据插入到sql server varchar(max)字段中并保留换行符。这些是地址,换行符对于解析地址数据至关重要。我在窗口中运行输出以查看结果是什么,并且在命令行中保留换行符但不插入到SQL Server中

    for dirpath, subdirs, files in os.walk(process):
    for file in files:
        dateadded = datetime.datetime.now()
        if file.endswith(('.dbf', '.DBF')):
            dbflocation = os.path.abspath(os.path.join(dirpath, file)).lower()
            if dbflocation.__contains__("\\xxx.dbf"):
                table = DBF(dbflocation, lowernames=True, char_decode_errors='ignore')
                for record in table.records or table.deleted:
                    rec1 = record['key1']
                    rec2 = str(record['xxx1'])
                    rec3 = str(record['xxx2'])
                    rec4 = record['xxx3']
                    rec5 = str(record['xxx4'])
                    rec6 = record['address1'] #address to parse
                    # if table.delete == True:
                    #    rec42 = 1
                    cursor.execute(
                        "insert into tblepiqclaims(key1,xxx1,xxx2,xxx3,xxx4,address1) values(?,?,?,?,?,?)",
                        rec1, rec2, rec3, rec4, rec5, rec6)
                    print rec6
            cnxn.commit()

1 个答案:

答案 0 :(得分:0)

您需要在varchar字段中定义转义字符。

例如:

输入=' 1#2#3'

print input.split('#')