简体   繁体   English

psycopg2:如何将 Python 应用程序中的数据添加到 Postgresql

[英]psycopg2: How to add data from a Python Application to Postgresql

This is a part of a project that captures news headlines, tracks leanings, and returns sentiment analysis (ie. positive, negative, and neutral).这是捕获新闻标题、跟踪倾向和回报情绪分析(即正面、负面和中性)的项目的一部分。 Everything works on the following code except for simply adding the captured data to a PostgreSQL table (FYI: CNN and USAToday are in the works as well, as I respect all news sources).除了简单地将捕获的数据添加到 PostgreSQL 表(仅供参考:CNN 和 USAToday 也在工作中,因为我尊重所有新闻来源)之外,一切都适用于以下代码。

Currently, the following error occurs:目前,出现以下错误:

"Exception has occurred: SyntaxError syntax error at or near "%" " “发生异常:在“%”处或附近出现 SyntaxError 语法错误”

I have tried to find examples of my specific situation, but all examples I see have the actual results in the insertion value, instead of the python values that contain the needed data.我试图找到我的具体情况的例子,但我看到的所有例子都有插入值的实际结果,而不是包含所需数据的 python 值。 I have attached the original table creation code as well:我还附上了原始表创建代码:

--create table
create table news_data (
timestamp timestamp,
user varchar(75),
url text,
site varchar(75),
lean varchar(75),
source varchar(75),
headline varchar(1000),
results text,
positive float,
negative float,
neutral float,

)

PYTHON CODE: PYTHON 代码:

#import the needed libraries
import os
import nltk
#nltk.download() #Only use once
import datetime
import getpass
import requests
import time
import numpy as np
import pandas as pd
import pandasql as psql
import lxml
from bs4 import BeautifulSoup
from nltk.sentiment.vader import SentimentIntensityAnalyzer as SIA
#from nltk.corpus.reader.plaintext import PlaintextCorpusReader as PCR
import psycopg2 as p2

#Application process
ts = datetime.datetime.now().date() #needs to be converted to a string
timestamp = ts.strftime("%d-%b-%Y (%H:%M:%S.%f)")
user = getpass.getuser()
url = 'https://www.foxnews.com/'
site = 'Fox News'
lean = 'Conservative'
source = requests.get(url)
soup = BeautifulSoup(source.content, 'lxml') #'source.content' is critical to success here
headline = soup.find('h2', class_='title title-color-default').text #apply sntiment analysis to headline
vader = SIA()
sentiment_dict = vader.polarity_scores(headline)
results = vader.polarity_scores(headline)
positive = (sentiment_dict['pos']*100)
negative = (sentiment_dict['neg']*100)
neutral = (sentiment_dict['neu']*100)


#lists
#sql_list = (timestamp, user, url, site, lean, headline, results, positive, negative, neutral)

#Postgresql connection
db_connection = p2.connect(user = 'PLACEHOLDER', password='PLACEHOLDER', database='PLACEHOLDER')
cursor = db_connection.cursor()
insertion = cursor.execute('''
INSERT INTO news_data VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
''')

record_to_insert = (timestamp, user, url, site, lean, headline, source, results, positive, negative, neutral)
cursor.execute(insertion, record_to_insert)


cursor.close()
db_connection.close()

        
#Testing
#print(headline)
#print(negative)

Even after changing the final lines into the following, I still get the following error:即使将最后几行更改为以下内容,我仍然收到以下错误:

Exception has occurred: SyntaxError syntax error at or near "%" LINE 1: ...ce, results, positive, negative, neutral) VALUES (%s, %s, %s...发生异常:在“%”第 1 行或附近出现 SyntaxError 语法错误:...ce, results, positive,negative,neutral) VALUES (%s, %s, %s...

Changed Code:更改代码:

cursor.execute("INSERT INTO news_data (timestamp, url, site, lean, headline, source, results, positive, negative, neutral) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s), (timestamp, url, site, lean, headline, results, positive, negative, neutral)")

cursor.commit()

cursor.close()

db_connection.close()
        
#Testing
#print(headline)
#print(negative)

Without looking at each data type in your statement, I can see that a few of them are strings.如果不查看语句中的每种数据类型,我可以看到其中一些是字符串。 Make sure you're surrounding string-type values with single-quotes where necessary.确保在必要时用单引号括住字符串类型的值。

insertion = cursor.execute('''
INSERT INTO news_data VALUES ('%s','%s', ...)
''')

ah, here's the relevant part of the code啊,这是代码的相关部分

#Postgresql connection
db_connection = p2.connect(user = 'PLACEHOLDER', password='PLACEHOLDER', database='PLACEHOLDER')
cursor = db_connection.cursor()

# hmm, this won't work.  You need to provide values for all those placeholders
insertion = cursor.execute('''
INSERT INTO news_data VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
''')

# oh, i see what you're up to.  You want this:

insertion = 'INSERT INTO news_data....'

record_to_insert = (timestamp, user, url, site, lean, headline, source, results, positive, negative, neutral)

cursor.execute(insertion, record_to_insert)

cursor.close()

# most likely, you'll also need
db_connection.commit()

db_connection.close()

Basically, psycopg2 does not support adding stored values from Python variables.基本上,psycopg2 不支持从 Python 变量中添加存储值。 You have to save the data to a CSV file first, have the code take the data from the CSV file, upload the data to PostgreSQL, then finally delete the CSV file once the process is complete. You have to save the data to a CSV file first, have the code take the data from the CSV file, upload the data to PostgreSQL, then finally delete the CSV file once the process is complete.

Hopefully, this will help someone in the future who is trying to accomplish something similar.希望这将有助于将来尝试完成类似事情的人。

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

相关问题 psycopg2 / python将数据从Postgresql复制到Amazon RedShift(postgresql) - psycopg2/python copy data from postgresql to Amazon RedShift(postgresql) 使用 psycopg2 将数据从 python 写入 postgreSQL 时遇到问题 - Trouble writing data from python to postgreSQL with psycopg2 如何在python中通过psycopg2模块取消存储在postgresql中的二进制数据? - how to unpickle binary data stored in postgresql by psycopg2 module in python? 尝试使用 psycopg2 将数据添加到 Postgresql 数据库 - Trying to add data to Postgresql database using psycopg2 psycopg2:在连接到PostgreSQL的Python中显示多列数据时出错 - psycopg2: error in display many columns of data in Python connecting to PostgreSQL 无法通过Python / psycopg2将数据插入Postgresql数据库 - Trouble inserting data into Postgresql db via Python/psycopg2 使用python psycopg2将数据插入PostgreSQL时出现插入错误 - Insert error while inserting data to PostgreSQL with python psycopg2 使用 psycopg2 将数据从 Postgresql 提取到 python 的最快/最有效的方法是什么 - What is the fastest/most efficient way to pull data from Postgresql to python with psycopg2 来自psycopg2 PostgreSQL查询的Python数组操作 - Python array manipulation from psycopg2 PostgreSQL query 使用psycopg2转换器从PostgreSQL检索bytea数据 - Using a psycopg2 converter to retrieve bytea data from PostgreSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM