網站首頁 編程語言 正文
Rows 是一個專門用于操作表格的第三方Python模塊。
只要通過 Rows 讀取 csv 文件,她就能生成可以被計算的 Python 對象。
相比于 pandas 的 pd.read_csv, 我認為 Rows 的優勢在于其易于理解的計算語法和各種方便的導出和轉換語法。它能非常方便地提取pdf中的文字、將csv轉換為sqlite文件、合并csv等,還能對csv文件執行sql語法,還是比較強大的。
當然,它的影響力肯定沒有 Pandas 大,不過了解一下吧,技多不壓身。
1.準備
開始之前,你要確保Python和pip已經成功安裝在電腦上,如果沒有,可以訪問這篇文章:超詳細Python安裝指南?進行安裝。
(可選1)?如果你用Python的目的是數據分析,可以直接安裝Anaconda,它內置了Python和pip.
(可選2)?此外,推薦大家用VSCode編輯器,它有許多的優點
請選擇以下任一種方式輸入命令安裝依賴:
1. Windows 環境 打開 Cmd (開始-運行-CMD)。
2. MacOS 環境 打開 Terminal (command+空格輸入Terminal)。
3. 如果你用的是 VSCode編輯器 或 Pycharm,可以直接使用界面下方的Terminal.
pip install rows
2.基本使用
通過下面這個小示例,你就能知道Rows的基本使用方法。
假設我們有這樣的一個csv表格數據:
state,city,inhabitants,area
AC,Acrelandia,12538,1807.92
AC,Assis Brasil,6072,4974.18
AC,Brasiléia,21398,3916.5
AC,Bujari,8471,3034.87
AC,Capixaba,8798,1702.58
[...]
RJ,Angra dos Reis,169511,825.09
RJ,Aperibé,10213,94.64
RJ,Araruama,112008,638.02
RJ,Areal,11423,110.92
RJ,Arma??o dos Búzios,27560,70.28
[...]
如果我們想要找出 state 為 RJ 并且人口大于 500000 的城市,只需要這么做:
import rows
cities = rows.import_from_csv("data/brazilian-cities.csv")
rio_biggest_cities = [
city for city in cities
if city.state == "RJ" and city.inhabitants > 500000
]
for city in rio_biggest_cities:
density = city.inhabitants / city.area
print(f"{city.city} ({density:5.2f} ppl/km2)")
和 Pandas 很像,但是語法比 Pandas 簡單,整個模塊也比 Pandas 輕量。
如果你想要自己新建一個"表格", 你可以這么寫:
from collections import OrderedDict
from rows import fields, Table
country_fields = OrderedDict([
("name", fields.TextField),
("population", fields.IntegerField),
])
countries = Table(fields=country_fields)
countries.append({"name": "Argentina", "population": "45101781"})
countries.append({"name": "Brazil", "population": "212392717"})
countries.append({"name": "Colombia", "population": "49849818"})
countries.append({"name": "Ecuador", "population": "17100444"})
countries.append({"name": "Peru", "population": "32933835"})
然后你可以迭代它:
for country in countries:
print(country)
# Result:
# Row(name='Argentina', population=45101781)
# Row(name='Brazil', population=212392717)
# Row(name='Colombia', population=49849818)
# Row(name='Ecuador', population=17100444)
# Row(name='Peru', population=32933835)
# "Row" is a namedtuple created from `country_fields`
# We've added population as a string, the library automatically converted to
# integer so we can also sum:
countries_population = sum(country.population for country in countries)
print(countries_population) # prints 357378595
還可以將此表導出為 CSV 或任何其他支持的格式:
# 公眾號:Python實用寶典
import rows
rows.export_to_csv(countries, "some-LA-countries.csv")
# html
rows.export_to_html(legislators, "some-LA-countries.csv")
從字典導入到rows對象:
import rows
data = [
{"name": "Argentina", "population": "45101781"},
{"name": "Brazil", "population": "212392717"},
{"name": "Colombia", "population": "49849818"},
{"name": "Ecuador", "population": "17100444"},
{"name": "Peru", "population": "32933835"},
{"name": "Guyana", }, # Missing "population", will fill with `None`
]
table = rows.import_from_dicts(data)
print(table[-1]) # Can use indexes
# Result:
# Row(name='Guyana', population=None)
3.命令行工具
除了寫Python代碼外,你還可以直接使用Rows的命令行工具,下面介紹幾個可能會經常被用到的工具。
讀取pdf文件內的文字并保存為文件:
# 需要提前安裝: pip install rows[pdf]
URL="http://www.imprensaoficial.rr.gov.br/app/_edicoes/2018/01/doe-20180131.pdf"
rows pdf-to-text $URL result.txt # 保存到文件 顯示進度條
rows pdf-to-text --quiet $URL result.txt # 保存到文件 不顯示進度條
rows pdf-to-text --pages=1,2,3 $URL # 輸出三頁到終端
rows pdf-to-text --pages=1-3 $URL # 輸出三頁到終端(使用 - 范圍符)
將csv轉化為sqlite:
rows csv2sqlite \
--dialect=excel \
--input-encoding=latin1 \
file1.csv file2.csv \
result.sqlite
合并多個csv文件:
rows csv-merge \
file1.csv file2.csv.bz2 file3.csv.xz \
result.csv.gz
對csv執行sql搜索:
# needs: pip install rows[html]
rows query \
"SELECT * FROM table1 WHERE inhabitants > 1000000" \
data/brazilian-cities.csv \
--output=data/result.html
其他更多功能,請見Rows官方文檔:
http://turicas.info/rows
原文鏈接:https://blog.csdn.net/u010751000/article/details/126635146
相關推薦
- 2022-08-30 new Socket(host, port)阻塞解決
- 2022-08-06 winform把Office轉成PDF文件_C#教程
- 2022-06-09 ASP.NET?Core使用EF創建模型(包含屬性、排除屬性、主鍵和生成值)_實用技巧
- 2023-11-22 Linux fatal: unable to access ‘https://github xxxx
- 2022-10-13 Python線性表種的單鏈表詳解_python
- 2022-06-02 python套接字socket通信_python
- 2022-11-21 Qt實現小功能之圓形進度條的方法詳解_C 語言
- 2022-05-17 docker停止某個容器
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支