python连接postgresql查询数据表中某一列数据,想把查询得到的数据存放在一个list中
import glob
import psycopg2 as psy
import os
filepattern = r'E:\study\Python\argo\202206.tar\202206\202206\*_*.dat'
filelist = glob.glob(filepattern)
"""连接库"""
host = 'localhost'
port = 5432
dbname = 'postgres'
user = 'postgres'
password = '123'
conn = psy.connect(host=host, port=port, dbname=dbname, user=user, password=password)
cursor = conn.cursor()
sql = '''
SELECT pressure FROM tempsalt
WHERE platformnumber = %s
'''
cursor.execute(sql, [1901302])
print(cursor)
results=cursor.fetchall()
for items in results:
pre=items[0]
print(pre)
4.5
7.1
10.5
15.4
19.6
24.6
30.4
34.3
39.8
44.9
49.8
54.6
59.6
64.6
69.4
74.6
80.5
85.2
89.9
93.6
97.8
110.3
118.5
130.6
140.1
149.9
160.5
170.2
180.4
190.4
200.5
209.8
220.3
229.5
240.5
250.4
260.0
269.9
280.3
290.2
300.2
310.1
319.9
330.0
339.9
349.9
359.9
380.5
400.1
420.2
439.9
459.8
479.7
500.3
549.7
599.9
648.3
700.5
800.0
900.5
999.2
1099.6
1200.3
1298.0
1399.8
1497.4
1599.9
1700.5
1800.4
1900.3
进程已结束,退出代码0
想达到这样的效果:[4.5,7.1,10.5,15.4,19.6,24.6,30.4,34.3,39.8]
这个简单,代码如下,有帮助的话采纳一下哦!
import glob
import psycopg2 as psy
import os
filepattern = r'E:\study\Python\argo\202206.tar\202206\202206\*_*.dat'
filelist = glob.glob(filepattern)
"""连接库"""
host = 'localhost'
port = 5432
dbname = 'postgres'
user = 'postgres'
password = '123'
conn = psy.connect(host=host, port=port, dbname=dbname, user=user, password=password)
cursor = conn.cursor()
sql = '''
SELECT pressure FROM tempsalt
WHERE platformnumber = %s
'''
cursor.execute(sql, [1901302])
print(cursor)
results = cursor.fetchall()
new_results = []
for items in results:
new_results.append(items[0])
print(new_results)
定义一个list=[]在for循环上面,然后在for循环下面加上list.append(pre)就行了
import glob
import psycopg2 as psy
import os
filepattern = r'E:\study\Python\argo\202206.tar\202206\202206\*_*.dat'
filelist = glob.glob(filepattern)
"""连接库"""
host = 'localhost'
port = 5432
dbname = 'postgres'
user = 'postgres'
password = '123'
conn = psy.connect(host=host, port=port, dbname=dbname, user=user, password=password)
cursor = conn.cursor()
sql = '''
SELECT pressure FROM tempsalt
WHERE platformnumber = %s
'''
cursor.execute(sql, [1901302])
print(cursor)
pressure_list = []
results=cursor.fetchall()
for items in results:
pressure_list.append(items[0])
print(pressure_list )
定义一个list = [],然后,在循环中用list.append(items[0])