Python 2列上的数组_agg,输出未识别为列表
我在Python中使用psycopg2的一个Postgresql查询中使用了array_agg 我发现的问题是当循环遍历结果行时。 查询生成的列不理解一列是列表列表。相反,它认为这是一个str列表 以下是数据库的一个示例:Python 2列上的数组_agg,输出未识别为列表,python,arrays,postgresql,list,psycopg2,Python,Arrays,Postgresql,List,Psycopg2,我在Python中使用psycopg2的一个Postgresql查询中使用了array_agg 我发现的问题是当循环遍历结果行时。 查询生成的列不理解一列是列表列表。相反,它认为这是一个str列表 以下是数据库的一个示例: +---------+-------------+-----------------+ | student | grade_value | grade_comment | +---------+-------------+-----------------+ | Paul
+---------+-------------+-----------------+
| student | grade_value | grade_comment |
+---------+-------------+-----------------+
| Paul | 1 | Very good |
| John | 1 | Very good |
| John | 2 | Average |
| Mark | 1 | Very good |
| Mark | 3 | Could do better |
| Mark | 1 | Very good |
+---------+-------------+-----------------+
我的问题是:
connection = psycopg2.connect(ls.CONNECTION_STRING)
cursor = connection.cursor(cursor_factory=RealDictCursor)
cursor.execute(
"SELECT student, array_agg('(' || grade_value || ',' || grade_comment || ')') as grades"
"FROM random_table"
"GROUP BY student"
)
students_grades = cursor.fetchall()
# This returns something like: RealDictRow([('student', John), ('grades', ['(1,Very good)', '(2,Average)'])]), RealDictRow([('student', Paul), ('grades', ['(1,Very good)'])])
for student in students_grades:
for grade in student['grades']:
print(grade)
print(type(grade))
末尾的打印(等级)值的格式如下:(1,非常好)
但是查询说类型是字符串。因此,我无法通过键入grade[1]来访问成绩注释。它认为分数是一根弦
您知道如何修复此问题吗?拆分字符串,使其成为一个列表。根据需要应用预处理
for grade in student['grades']:
# Let's say grade = "(A,Very good)"
g = grade.replace('(', '').replace(')','') # g --> "A,Very good"
l = g.split(',') # l --> ["A", "Very good"]
print(l[1]) # --> "Very good"
在IPython中测试:
In [1]: grade = "(A,Very good)"
In [2]: g = grade.replace('(', '').replace(')', '')
In [3]: l = g.split(',')
In [4]: print(l[0])
A
In [5]: print(l[1])
Very good
编辑:
In [4]: grade = "(A,Very good, but needs some improvement.)"
In [5]: g = grade.replace('(', '').replace(')', '')
In [6]: l = g.split(',', 1)
In [7]: print(l[0])
A
In [8]: print(l[1])
Very good, but needs some improvement.
您不需要将字符串集中在array\u agg中,只需将列传递到数组中即可。
Psycopg2将在postgres和python之间转换正确的类型,正如您在输出中看到的那样
students\u grade['grades']
作为列表获取:
cursor.execute("""
select
student,
array_agg(array[grade_value, grade_comment]) as grades
from random_table
group by student""")
students_grades = cursor.fetchall()
for students_grade in students_grades:
print(students_grade['student'])
for grade in students_grade['grades']:
print("%s %s" % (type(grade), grade))
输出:
Tom
<class 'list'> ['2', 'Good']
<class 'list'> ['3', 'Very good']
John
<class 'list'> ['2', 'Very good']
Tom
<class 'dict'> {'grade': 2, 'comment': ['Good']}
<class 'dict'> {'grade': 3, 'comment': ['Very good']}
John
<class 'dict'> {'grade': 2, 'comment': ['Very good']}
输出:
Tom
<class 'list'> ['2', 'Good']
<class 'list'> ['3', 'Very good']
John
<class 'list'> ['2', 'Very good']
Tom
<class 'dict'> {'grade': 2, 'comment': ['Good']}
<class 'dict'> {'grade': 3, 'comment': ['Very good']}
John
<class 'dict'> {'grade': 2, 'comment': ['Very good']}
Tom
{'grade':2,'comment':['Good']}
{'grade':3,'comment':['Very good']}
约翰
{'grade':2,'comment':['Very good']}
谢谢你的建议。这个问题是grade_评论可以包含“,”本身,所以我认为它会中断。如果列表中只有两个元素,那么在第一次出现时就拆分。请参阅编辑。这似乎可以完美地处理相同类型的两列。但是,我有以下问题:“连接到PostgreSQL数组类型时出错整数和文本无法匹配”这主要是因为grade_值为int,grade_注释为字符串。经过编辑,在不同类型的情况下,可以使用“json_agg()”。或者将int强制转换为text/varchar:array\u agg(array[grade\u value::text,grade\u comment])作为grades
。