如何处理SQL和Python中的换行符以适当地返回所有行? [英] How to handle a line break in SQL and Python to appropriately return all lines?

查看:82
本文介绍了如何处理SQL和Python中的换行符以适当地返回所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带换行符的字符串,例如在GUI工具中输入此处的第一行[linebreak]下一个,并将其读入SQL Server数据库中,在该数据库中它看起来像是一个行字符串,即此处的第一行下一个" .但是,如果该字段是由Python读取的,则返回为 NEXT ONEnehere .似乎换行符是通过某种方式注册的,但是由于两行都写在同一行上,所以处理是错误的.如何处理此链GUI-SQL-Python中的换行符以适当地返回两行?

A string with a line break, e.g. first line here[linebreak]NEXT ONE is entered in a GUI-tool and read into an SQL server database where it appears to be a one line string, i.e. first line here NEXT ONE. However, if this field is read by Python, it is returned as NEXT ONEne here. It seems that the line break is registered somehow, but the handling is wrong as both lines are written on the same line. How to handle the line break in this chain GUI-SQL-Python to appropriately return both lines?

推荐答案

如果使用MySQL,则可以使用mysqldb的escape_string方法:

In case that you use MySQL you can use the escape_string method of mysqldb: https://dev.mysql.com/doc/connector-python/en/connector-python-api-cext-escape-string.html

这将在将换行符存储到数据库之前转义换行符(例如 \ n ).

This will escape the new line character (e.g. \n) before storing it into your database.

考虑使用调试器(例如 pdb

Consider using a debugger (e.g.pdb or pudb) to examin the string you store into and the one you receive from the database.

这篇关于如何处理SQL和Python中的换行符以适当地返回所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆