8ctopus/nano-csv
Composer 安装命令:
composer require 8ctopus/nano-csv
包简介
Read csv and Excel xlsx files
README 文档
README
Parse csv and Excel xlsx files
features
- parse csv and Excel xlsx files
- no dependencies, fast and low memory footprint
- very small code base: 1100 lines of code
- auto detect file encoding and line endings
- auto detect csv separator, enclosure and header presence
- unicode support
install and demo
composer require 8ctopus/nano-csv
- Simple csv parsing
use Oct8pus\CSV\CSV; require_once __DIR__ . '/vendor/autoload.php'; $csv = new CSV(__DIR__ .'/samples/ascii-mac-header.csv'); echo $csv ->autoDetect() . PHP_EOL; while ($row = $csv->readNextRow()) { echo implode(', ', $row) . PHP_EOL; }
file: /dev/github/nano-csv/samples/ascii-mac-header.csv size: 500 BOM: None encoding: ASCII line ending: Mac lines count: 9 separator: , enclosure: " header: true rows count: 8 columns (13): Month, Average, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 May, 0.1, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0 Jun, 0.5, 2, 1, 1, 0, 0, 1, 1, 2, 2, 0, 1 Jul, 0.7, 5, 1, 1, 2, 0, 1, 3, 0, 2, 2, 1 Aug, 2.3, 6, 3, 2, 4, 4, 4, 7, 8, 2, 2, 3 Sep, 3.5, 6, 4, 7, 4, 2, 8, 5, 2, 5, 2, 5 Oct, 2.0, 8, 0, 1, 3, 2, 5, 1, 5, 2, 3, 0 Nov, 0.5, 3, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1 Dec, 0.0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1
- And some handy options
$csv = new CSV(__DIR__ .'/samples/ascii-mac-header.csv'); $csv ->autoDetect() // convert string to number ->setConvertNumbers(true) // return associative array ->setAssociativeArray(true); $average = 0; while ($row = $csv->readNextRow()) { $average += $row['Average']; } echo "Average from May to Dec: {$average}" . PHP_EOL;
Average from May to Dec: 9.6
- Excel xlsx parsing
use Oct8pus\CSV\XLSX; $xls = new XLSX(__DIR__ .'/samples/test.xlsx'); echo $xls ->autoDetect() . PHP_EOL; while ($row = $xls->readNextRow()) { echo implode(', ', $row) . PHP_EOL; }
file: K:\dev\github\nano-csv/samples\test.csv size: 174 BOM: UTF-8 encoding: UTF-8 line ending: Linux lines count: 9 separator: , enclosure: none header: true rows count: 7 columns (5): name, class, weight, empty, height cat, mammal, 8, , 0.2 rabbit, mammal, 0.6, , 0.2 dog, mammal, 20, , 0.7 puma, mammal, 30, , 0.6 pinguin, bird, 10, , 0.4 bear, mammal, 300, , 1 bat, mammal, 0.1, , 0.1
Also look at the demo-* files.
tests
composer test
clean code
composer fix
composer fix-risky
todo
- use readonly properties
- detect escape char
- refactor read
- make a really tricky test file - detect escape character within enclosures
- compare performance against most popular csv parsers
credits
https://filesamples.com/formats/csv
https://eforexcel.com/wp/downloads-18-sample-csv-files-data-sets-for-testing-sales/
8ctopus/nano-csv 适用场景与选型建议
8ctopus/nano-csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 107 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「excel」 「xlsx」 「csv」 「parser」 「import」 「read」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 8ctopus/nano-csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 8ctopus/nano-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 8ctopus/nano-csv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHP Excel Library
laravel facade to read/write csv file
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
统计信息
- 总下载量: 107
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-07