简体   繁体   English

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

[英]Preserve CR LF(New Lines) On Insert Into SQL Server

I am attempting to insert data into a sql server varchar(max) field and retain line breaks. 我正在尝试将数据插入sql server varchar(max)字段并保留换行符。 These are addresses and the line breaks will be vital to parsing out address data. 这些是地址,换行符对于解析地址数据至关重要。 I am running the output in a window to see what the results are and the line breaks are maintained in the command line but not on insert into SQL Server 我在一个窗口中运行输出以查看结果是什么,并且在命令行中保留了换行符,但未在插入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()

You need to define the escape character in the varchar field. 您需要在varchar字段中定义转义字符。

eg: 例如:

input = '1#2#3' 输入='1#2#3'

print input.split('#') 打印input.split('#')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM