繁体   English   中英

值错误:无法将 [] 转换为 Excel

[英]ValueError: Cannot convert [] to Excel

我正在尝试运行连接到 postrgres 数据库的程序,运行查询并将此结果保存在 excel 电子表格中。

我面临的问题是在我运行查询时。 'memberof' 列的结果有一个空的 {}。

  rolname   | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolconnlimit | rolvaliduntil | memberof | rolreplication | rolbypassrls
------------+----------+------------+---------------+-------------+-------------+--------------+---------------+----------+----------------+--------------
 joe        | f        | t          | f             | f           | t           |           -1 |               | {}       | f              | f
 postgres   | t        | t          | t             | t           | t           |           -1 |               | {}       | t              | t
 test_joe   | f        | t          | f             | f           | t           |           -1 |               | {}       | f              | f

因此,我的代码返回此错误输出。

Traceback (most recent call last):
  File "/path/to/file.py", line 55, in <module>
    GetPostgresqlData()
  File "/path/to/file.py", line 52, in GetPostgresqlData
    ws.append(row)
  File "/path/to/.local/lib/python3.9/site-packages/openpyxl/worksheet/worksheet.py", line 665, in append
    cell = Cell(self, row=row_idx, column=col_idx, value=content)
  File "/path/to/.local/lib/python3.9/site-packages/openpyxl/cell/cell.py", line 116, in __init__
    self.value = value
  File "/path/to/.local/lib/python3.9/site-packages/openpyxl/cell/cell.py", line 215, in value
    self._bind_value(value)
  File "/path/to/.local/lib/python3.9/site-packages/openpyxl/cell/cell.py", line 184, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert [] to Excel

我该怎么做才能将此列和其他列的结果写入excel电子表格?

这是我针对我的测试数据库运行的查询。

SELECT r.rolname, r.rolsuper, r.rolinherit,
  r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
  r.rolconnlimit, r.rolvaliduntil,
  ARRAY(SELECT b.rolname
        FROM pg_catalog.pg_auth_members m
        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
        WHERE m.member = r.oid) as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
WHERE r.rolname !~ '^pg_'
ORDER BY 1;

这是我的代码。

def GetPostgresqlData():

    wb = Workbook()
    wb.remove(wb['Sheet'])
    ws = wb.create_sheet(0)
    ws.title = 'Titulo 01'

    hosts = open('serverlist.csv', 'r')
    hosts = hosts.readlines()[1:]

    for i in range(len(filequery[1:])):
        with open(filequery[i+1], 'r') as postgresql_query:
            query = postgresql_query.read()

            for row in hosts:
                command_Array = row.strip('\n').split(',')
                db_host = command_Array[0]
                db_port = command_Array[1]
                db_name = command_Array[2]
                db_user = command_Array[3]
                db_password = command_Array[4]

                postgresql_connection = psycopg2.connect(user=db_user,
                                                         password=db_password,
                                                         host=db_host,
                                                         port=db_port,
                                                         database=db_name)

                cursor = postgresql_connection.cursor()
                cursor.execute(query)
                colnames = [desc[0] for desc in cursor.description] 
                records = cursor.fetchall()

            ws.append(colnames)
            for row in records:
                ws.append(row)
                wb.save("postgresql.xlsx")

GetPostgresqlData()

Charlie Chuck 在第一条评论中指出的我面临的问题是我无法在单个单元格中保存列表。 所以我通过将数组转换为字符串来解决这个问题,如下所示。

SELECT r.rolname, r.rolsuper, r.rolinherit,
  r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
  r.rolconnlimit, r.rolvaliduntil,
  array_to_string(ARRAY(SELECT b.rolname
        FROM pg_catalog.pg_auth_members m
        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
        WHERE m.member = r.oid), ',', '*') as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
WHERE r.rolname !~ '^pg_'
ORDER BY 1

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM