luchaninov/csv-file-loader
Composer 安装命令:
composer require luchaninov/csv-file-loader
包简介
Load CSV & TSV files and strings. Using generators to minimize memory usage
关键字:
README 文档
README
Load CSV & TSV files and strings using PHP generators. It uses memory like fopen but requires less code.
How to Install
Install the luchaninov/csv-file-loader package using composer:
$ composer require luchaninov/csv-file-loader
Basic Usage
$loader = new CsvFileLoader('/path/to/your_data.csv'); foreach ($loader->getItems() as $item) { var_dump($item); // do something here }
If you have CSV-file
id,name,surname
1,Jack,Black
2,John,Doe
you'll get 2 items
['id' => '1', 'name' => 'Jack', 'surname' => 'Black']
['id' => '2', 'name' => 'John', 'surname' => 'Doe']
It uses fgetcsv function so it understands enclosed values like
item1,"item2,still item2",item3
and even
item1,"item2
still item2",item3
If you have simple string, no need to save it to the file. Use
(new CsvStringLoader($s))->getItemsArray(); (new TsvStringLoader($s))->getItemsArray(); (new AutoStringLoader($s))->getItemsArray();
Advanced Usage
If file is not large you can load all items at once without generators using getItemsArray().
If you have custom delimiters use setDelimiter like $loader->setDelimiter(';'). Same with encloser - setEncloser.
Default delimiter is , for CsvFileLoader and \t for TsvFileLoader; default encloser is ".
If you are not sure - use delimiter auto, it detects delimiter automatically by the first 10,000 characters.
If you have TSV instead of CSV you can set use setDelimiter("\t") or use TsvFileLoader.
If you are not sure - use AutoFileLoader, it detects delimiter automatically.
By default it assumes that the first row of the file contains headers - it doesn't return this row as item but uses as keys for next rows. If you don't have headers in the first row - you can:
- set your own keys -
setHeaders(['key1', 'key2', ...]) - use numerical keys
[0, 1, 2, ...]-setHeaders(false)
If there are more cols in some rows than there are cols in headers then they are truncated.
If you prefer to add extra values with numerical keys use setAddUnknownColumns(true).
To count items use countItems(). In case of CSV it's not always the same with rows count - wc -l, because one item can have several rows.
You can use same loader to load several files - $loader->setFilename('other_file.csv'). If you iterate during some file when calling
setFilename then there will be no more items from the first file, foreach will just finish.
Code is very simple - look at sources and tests.
TxtFileLoader
If you have simple text file use TxtFileLoader.
It makes from file
text1
text2
text3
array ['text1', 'text2', 'text3'].
setSkipEmptyRows
Skips empty rows or containing only whitespaces trim($s) === ''. Default: true.
setSkipComments
Skips rows that start with # or \s+#. Default: false.
luchaninov/csv-file-loader 适用场景与选型建议
luchaninov/csv-file-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39.29k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2015 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「csv」 「loader」 「tsv」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 luchaninov/csv-file-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 luchaninov/csv-file-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 luchaninov/csv-file-loader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Symfony2 bundle that provides features to load assets
An environment variable convenience library extension for vlucas/phpdotenv
PhpStratum SQLite: A stored procedure loader and wrapper generator for SQLite
Zend Framework 1 Loader package
A data loader command base class for Laravel
统计信息
- 总下载量: 39.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-30