Goodby CSV 项目使用教程

Goodby CSV 项目使用教程

csvGoodby CSV is a high memory efficient flexible and extendable open-source CSV import/export library for PHP 5.3. 1. Memory Management Free This library designed for memory unbreakable. It will not be accumulated in the memory whole rows. The importer read CSV file and execute callback function line by line. 2. Multibyte support This library supports mulitbyte input/output: for example, SJIS-win, EUC-JP and UTF-8. 3. Ready to Use for Enterprise Applications Goodby CSV is fully unit-tested. The library is stable and ready to be used in large projects like enterprise applications.项目地址:https://gitcode.com/gh_mirrors/csv/csv

1. 项目的目录结构及介绍

Goodby CSV 项目的目录结构如下:

goodby/csv/
├── AUTHORS
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── composer.json
├── phpunit.xml.dist
├── src/
│   └── Goodby/
│       └── CSV/
│           ├── Collection/
│           ├── Database/
│           ├── Export/
│           ├── Import/
│           └── Protocol/
├── tests/
│   └── Goodby/
│       └── CSV/
│           ├── Export/
│           ├── Import/
│           └── Protocol/
└── vendor/

目录介绍

  • AUTHORS: 项目作者列表。
  • CHANGELOG.md: 项目更新日志。
  • CONTRIBUTING.md: 贡献指南。
  • LICENSE: 项目许可证。
  • README.md: 项目说明文档。
  • composer.json: Composer 配置文件。
  • phpunit.xml.dist: PHPUnit 配置文件。
  • src/: 源代码目录,包含主要的 CSV 处理类和接口。
    • Goodby/CSV/: CSV 处理的核心代码。
      • Collection/: 数据集合处理相关类。
      • Database/: 数据库相关类。
      • Export/: 导出 CSV 文件的相关类。
      • Import/: 导入 CSV 文件的相关类。
      • Protocol/: 协议相关类。
  • tests/: 测试代码目录,包含单元测试和功能测试。
    • Goodby/CSV/: 测试代码。
      • Export/: 导出 CSV 文件的测试。
      • Import/: 导入 CSV 文件的测试。
      • Protocol/: 协议相关测试。
  • vendor/: Composer 依赖包目录。

2. 项目的启动文件介绍

Goodby CSV 项目没有传统意义上的“启动文件”,因为它是一个库,需要在你自己的项目中引入和使用。你可以通过 Composer 安装后,在你的 PHP 脚本中引入自动加载文件,然后使用 Goodby CSV 提供的类和方法。

示例

require 'vendor/autoload.php';

use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;

$config = new LexerConfig();
$lexer = new Lexer($config);

$interpreter = new Interpreter();
$interpreter->addObserver(function(array $row) {
    // 处理每一行数据
    print_r($row);
});

$lexer->parse('input.csv', $interpreter);

3. 项目的配置文件介绍

Goodby CSV 项目的配置主要通过 LexerConfigExporterConfig 类来完成。这些配置类允许你设置 CSV 文件的分隔符、编码、是否包含头部等参数。

LexerConfig 示例

use Goodby\CSV\Import\Standard\LexerConfig;

$config = new LexerConfig();
$config->setDelimiter(',') // 设置分隔符
       ->setEnclosure('"') // 设置包围符
       ->setEscape('\\')  // 设置转义符
       ->setToCharset('UTF-8') // 设置目标编码
       ->setFromCharset('SJIS-win'); // 设置源编码

ExporterConfig 示例

use Goodby\CSV\Export\Standard\ExporterConfig;

$config = new ExporterConfig();
$config->setDelimiter(',') // 设置分隔符
       ->setEnclosure('"') // 设置包围符
       ->setEscape('\\')  // 设置转义符
       ->setNewline("\r\n") // 设置换行符
       ->setFileMode(ExporterConfig::FILE_MODE_WRITE); // 设置文件模式

通过这些配置,你可以灵活地处理不同格式和编码的 CSV 文件。

csvGoodby CSV is a high memory efficient flexible and extendable open-source CSV import/export library for PHP 5.3. 1. Memory Management Free This library designed for memory unbreakable. It will not be accumulated in the memory whole rows. The importer read CSV file and execute callback function line by line. 2. Multibyte support This library supports mulitbyte input/output: for example, SJIS-win, EUC-JP and UTF-8. 3. Ready to Use for Enterprise Applications Goodby CSV is fully unit-tested. The library is stable and ready to be used in large projects like enterprise applications.项目地址:https://gitcode.com/gh_mirrors/csv/csv

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨女嫚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值