Home > database >  Python to postgresql it write error string with a comma
Python to postgresql it write error string with a comma

Time:09-30

 import urllib. Request 
The import re
The import psycopg2 as psy
Conn=psy. Connect (database='test1', user='postgres' and password=' 091297 ', the host='127.0.0.1', the port='5432')
Cur=conn. Cursor ()
Try:
Cur. Execute (' the create table unable to wikipedia (num int, STR text); ')
Except:
Pass
MIT ()
conn.comDef get_html (url) :
The req=urllib. Request. The request (url)
The req. Add_header (' the user-agent ', 'Mozilla/5.0 (Windows NT 5.1; The rv: 43.0) Gecko/20100101 Firefox/43.0 ')
The response=urllib. Request. Urlopen (the req)
HTML=response. The read (). The decode (' utf-8)
Return the HTML

Def get_list (HTML) :
List1=re. The.findall (r '\ & lt; Div \ & gt; \ n {4}. +? {3} \ \ n & lt;/div \ & gt; ', HTML)
For each in list1:
Str1=re search (r '\ & lt; Span \ & gt; . + \ & lt;/span \ & gt; ', each). Group () [6, 7]
Str1=str1. Replace (' & lt; Br/& gt; ', '\ n')
Print (str1)
Global I
I=I + 1
Print (I)
Cur. Execute (" 'insert into blunders that wikipedia values (% d, % s); "' % (I, str1)) the str1 # # is a comma will be as a separator, a field into two fields, and then an error
MIT ()
conn.com
I=0
For page in range (1, 35) :
Print (page)
HTML=get_html (' http://www.qiushibaike.com/text/page/'+ STR (page) +'/')
Get_list (HTML)



The error message below

C: \ Users \ \ Administrator \ \ AppData \ Local \ designed \ Python Python35 \ Python exe C:/Users/Administrator/Desktop/Python practice file/indiscretions encyclopedia. Py
1
Back in school, mom and dad specially took a day off to send me to the railway station, with two lines of tears, a deputy from the appearance, I can't live I also sadness, pull the suitcase don't know what to say, half a day to a "mom and dad for a while go home be careful," dad says, "no, I talk to your mother go to bubble hot spring"
1
Traceback (the most recent call last) :
File "C:/Users/Administrator/Desktop/python File/off an practice. Py", line 34, in & lt; module>
Get_list (HTML)
File "C:/Users/Administrator/Desktop/python File/off an practice. Py", line 27, in get_list
Cur. Execute (' insert into blunders that wikipedia values (% d, % s); '% (I, str1))
Psycopg2. ProgrammingError: error: field "back in school, mom and dad specially took a day off to go to the railway station to send me, that" there is no
LINE 1: insert into blunders that wikipedia values (1, a school, mom and dad specially took a day off spent...

^


The Process finished with exit code 1

Specially test
 & gt;> Cur. Execute (' insert into blunders that wikipedia values (% d, % s); '% (1,' 1234 ')) 
> Cur. Execute (' insert into blunders that wikipedia values (% d, % s); '% 1' 12, 34)) # # more than a comma
Traceback (the most recent call last) :
The File "& lt; Pyshell# 12 & gt;" , the line 1, the in & lt; module>
Cur. Execute (' insert into blunders that wikipedia values (% d, % s); '% 1' 12, 34))
Psycopg2. ProgrammingError: error: INSERT the expression of more than a specified number of field
LINE 1: insert into blunders that wikipedia values,12,34 (1);


A long characters, the text inside a comma is inevitable, how can not be seen as the field separator?

CodePudding user response:

Str1 surrounded with a $$,

Is the content of the str1 $$$$here

CodePudding user response:

You this is a question of python, "it must be a string with a comma, you put the comma escape down should be no problem
  • Related