如何在字符串数据列[Python]中计算SQLAlchemy中的数据?

2024-06-01 14:31:36 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我正在使用SQL炼金术计算人口总数。我想对填充列求和,但我有一个问题,即数据由点分隔符组成

如何替换SQL Alchemy中的点分隔符?这是我的密码:

stmt = select([demography.columns.nama, demography.columns.populasi]) stmt = stmt.order_by(demography.columns.pulau) results = connection.execute(stmt).fetchall() print(*results, sep='\n')

('Ibukota Jakarta', '9.607.787') ('Jawa Barat', '43.053.732') ('Jawa Tengah', '32.382.657') ('Daerah Istimewa Yogyakarta', '3.457.491') ('Jawa Timur', '37.476.757') ('Banten', '10.632.166')


Tags: columns数据密码sqlselectresults炼金术分隔符
1条回答
网友
1楼 · 发布于 2024-06-01 14:31:36

我要做的不是在sqlalchemy查询中执行,而是首先获得结果,然后进行解析和计算,如下所示:

stmt = select([demography.columns.nama, demography.columns.populasi])
stmt = stmt.order_by(demography.columns.pulau)
results = connection.execute(stmt).fetchall()

for res in results:
    population = res[1] #this is the index for population count
    population_list.append(int(''.join(population.split('.'))))
print(sum(population_list)) #prints 136610590

相关问题 更多 >