python replace string to insert MySQL

Posted under » MySQL » Python on 08 Jul 2021

When you use CKeditor and the data need to go to another database field, you will need to replace the carriage return and the backslash eg.

a_title = '''

Water is poured at a constant rate of \(10~\text{cm}^3\) into an inverted right cone with semi-vertical angle \(45^{\circ}\) as shown in the diagram. After \(t\) seconds, the depth of water in the cone is \(h~\text{cm}\).


[It is given that volume of cone = \(\frac{1}{3}\pi r^2h\)]

''' c_title = a_title.replace('\n', ' ').replace("\r","").replace('\\','\\\\') print(c_title)

You will notice that for multiple lines, you will have to put 3 x ' or ''' instead of just one. It is good to replace both \n and \r because only \n might not do it if you are using windows. The clean data can then be inserted via mysql.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS