Skip to main content

不要生气,得到结果

项目描述

不要生气,得到results

表格数据和 SQL 供没有时间关心的人使用。

在 xlsx、xls、csv、python、postgres 之间移动并轻松返回。

特征:

  • 零样板数据库创建、连接和查询。
  • 加载/整理/转换 csv 和 excel 数据。
  • 自动检测列类型,只需很少或无需手动指定即可加载数据。
  • 数据库结果的强大的多列、多顺序键集分页。
  • 架构同步。

限制

  • 仅限 Python 3.6+、PostgreSQL 10+。许多功能可以与其他数据库一起使用,但很多功能不能。只需使用 Postgres!

安装

results在 PyPI 上pip使用或任何(许多)Python 包管理器安装它。

设想

有人给你一个凌乱的 csv 或 excel 文件。你需要加载它,清理它,将它放入数据库,查询它,从中创建一个数据透视表,然后将数据透视表作为 csv 发送给某人。

results来这里是为了快速完成这类事情,并且尽可能少地大惊小怪。

让我们来看看。

首先,加载和清理:

import results

# load a csv (in this example, some airport data)
sheet = results.from_file("airports.csv")

# do general cleanup
sheet.standardize_spaces()
sheet.set_blanks_to_none()

# give the keys lowercase-with-underscore names to keep the database happy
cleaned = sheet.with_standardized_keys()

然后,创建一个数据库:

# create a database
DB = "postgresql:///example"

db = results.db(DB)

# create it if it doesn't exist
db.create_database()

然后为数据创建一个表,自动猜测列并创建一个表来匹配。

# guess the column types
guessed = cleaned.guessed_sql_column_types()

# create a table for the data
create_table_statement = results.create_table_statement("data", guessed)

# create or auto-update the table structure in the database
# syncing requires a copy of postgres running locally with your current user set up as superuser
db.sync_db_structure_to_definition(create_table_statement, confirm=False)

然后插入数据,自由查询。

# insert the data. you can also do upserts with upsert_on!
db.insert("data", cleaned)

# show recent airfreight numbers from the top 5 airports
# ss means "single statement"
query_result = db.ss(
    """
with top5 as (
    select
        foreignport, sum(freight_in_tonnes)
    from
        data
    where year >= 2010
    group by
        foreignport
    order by 2 desc
    limit 5
)

select
    year, foreignport, sum(freight_in_tonnes)
from
    data
where
    year >= 2010
    and foreignport in (select foreignport from top5)
group by 1, 2
order by 1, 2

"""
)

创建一个数据透视表,然后将其打印为 markdown 或将其保存为 csv。

# create a pivot table
pivot = query_result.pivoted()

# print the pivot table in markdown format
print(pivot.md)

输出:

|   year |   Auckland |    Dubai |   Hong Kong |   Kuala Lumpur |   Singapore |
|-------:|-----------:|---------:|------------:|---------------:|------------:|
|   2010 |     288997 | 145527   |      404735 |       226787   |      529407 |
|   2011 |     304628 | 169868   |      428990 |       244053   |      583921 |
|   2012 |     312828 | 259444   |      400596 |       272093   |      614155 |
|   2013 |     306783 | 257263   |      353895 |       272804   |      592886 |
|   2014 |     309318 | 244776   |      330521 |       261438   |      620419 |
|   2015 |     286202 | 263378   |      290292 |       252906   |      633862 |
|   2016 |     285973 | 236419   |      309556 |       175858   |      614172 |
|   2017 |     314405 | 226048   |      340216 |       199868   |      662505 |
|   2018 |     126712 |  91611.2 |      134540 |        74667.5 |      250653 |

将表格另存为 csv:

pivot.save_csv("2010s_freight_sources_top5.csv")

设计理念

  • 不惜一切代价避免样板。让它尽可能简单,但不要更简单。

  • 不要重新发明轮子:results使用 sqlalchemy 进行数据库连接,使用现有的 excel 解析库进行 excel 解析等。results将它们组合在一起,在上面撒一些糖,然后触手可及。

  • 吃你自己的狗粮:我们自己每天都用这个。

文档

这个 README.md 目前是所有内容 :( 但我们会尽快添加更多内容,我们保证!

学分

贡献

是的,请!

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

results-0.1.1588569394.tar.gz (19.0 kB 查看哈希

已上传 source

内置分布

results-0.1.1588569394-py3-none-any.whl (21.0 kB 查看哈希

已上传 py3