- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 PHP 电子表格库读取 CSV 文件。
我成功获取了数据,但它把分隔到数组中的逗号打错了。
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$spreadsheet = $reader->load('File.csv');
$sheetData = $spreadsheet->getActiveSheet()->toArray();
echo '<pre>';
print_r($sheetData);
当我看到输出时,它看起来像这样:
Array
(
[0] => Array
(
[0] => Id,Record,Book,Description,Date,Action
[1] =>
[2] =>
)
[1] => Array
(
[0] => 1306,5466,84,Item,04-05-2018 06
[1] => 26
[2] => 28,Auto Show
)
)
代替:
Array
(
[0] => Array
(
[0] => Id
[1] => Record
[2] => Book
[3] => Description
[4] => Date
[5] => Action
)
[1] => Array
(
[0] => 1306
[1] => 5466
[2] => 84
[3] => Item
[4] => 04-05-2018 06:26:28
[5] => Auto Show
)
)
提前谢谢你。
最佳答案
您是否尝试过强制行尾保持一致?我以前遇到过这个,在解析 CSV 之前必须添加一个 PHP ini 设置:
ini_set('auto_detect_line_endings',TRUE)
把它放在 $reader 之前
Edit via request in comments
这是我用于 CSV 的类,假设您至少有 PHP 5.5:
<?php
use Exception;
use InvalidArgumentException;
use SplFileObject;
use NoRewindIterator;
class LargeFile {
const ERROR_UNABLE = 'ERROR: Unable to open file';
const ERROR_TYPE = 'ERROR: Type must be "ByLength", "ByLine", or "Csv"';
protected $file;
protected $allowed_types = [ 'ByLine', 'ByLength', 'Csv' ];
/**
* LargeFile constructor.
*
* @param $filename
* @param string $mode
*
* @throws Exception
*
* Populates $file with new SPL File object instance.
*/
public function __construct($filename, $mode = 'r') {
if (!file_exists($filename)) {
$message = __METHOD__ . ' : ' . self::ERROR_UNABLE . PHP_EOL;
$message .= strip_tags($filename) . PHP_EOL;
throw new Exception($message);
}
$this->file = new SplFileObject($filename, $mode);
}
/**
* @return \Generator|int
*
* References SplFileObject method to read the file one line
* at a time with fgets.
*
* Suitable for smaller text files like Csvs and / or
* include line feeds.
*/
protected function fileIteratorByLine() {
$count = 0;
while (!$this->file->eof()) {
yield $this->file->fgets();
$count++;
}
return $count;
}
/**
* @param $numBytes
*
* @return \Generator|int
*
* References SplFileObject method to read the file one line
* at a time with freads.
*
* Suitable for larger binary files.
*/
protected function fileIteratorByLength($numBytes) {
$count = 0;
while (!$this->file->eof()) {
yield $this->file->fread($numBytes);
$count++;
}
return $count;
}
protected function fileIteratorCsv() {
$count = 0;
while (!$this->file->eof()) {
yield $this->file->fgetcsv();
$count++;
}
return $count;
}
/**
* @param string $type
* @param null $numBytes
*
* @return NoRewindIterator
*/
public function getIterator($type = 'ByLine', $numBytes = null) {
if (!in_array($type, $this->allowed_types)) {
$message = __METHOD__ . ' : ' . self::ERROR_TYPE . PHP_EOL;
throw new InvalidArgumentException($message);
}
$iterator = 'fileIterator' . $type;
return new NoRewindIterator($this->$iterator($numBytes));
}
}
你可以这样使用它:
$largeFile = new LargeFile($file);
// tell it use the CSV iterator
$iterator = $largeFile->getIterator('Csv');
// start iterator and pull out header row as $rawHeaders
$rawHeaders = $iterator->current();
$headerIterator = new \ArrayIterator;
foreach ($rawHeaders as $header) {
$headerIterator->append(strtolower("`$header`"));
}
$headers = $headerIterator->getArrayCopy();
// move pointer to the data rows
$iterator->next();
// loop through each row
foreach( $iterator as $row ) {
// do stuff with each row
print_r($row);
}
关于PHP 电子表格读取 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50110854/
我这里有一个工作示例。 https://developers.google.com/google-apps/spreadsheets/我需要从用户的谷歌文档下载一些文件。 import com.goo
我需要您在以下方面的帮助: 假设我们有 3 个不同的组:(A、B、C) 我们在每个组中有几个值范围: (Eg. A has 0 - 100, 101 - 200 while B has 0 - 200
谁能告诉我有什么方法可以从 python 连接到电子表格吗?我想从表单中存储一些数据并将其提交到谷歌电子表格。请帮助解决这个问题。我必须遵循哪些步骤? 提前致谢... 最佳答案 连接到 Google
我正在尝试计算 2019 年给定时间范围内的月数。 我的数据如下所示: 我将开始日期和结束日期列的格式更改为“日期”。 “以月为单位的持续时间”和输出列具有数字格式。 我接近它如下: 如果开始日期(年
在设计上有一点问题,希望我能在这里寻求建议。接受 grails 可能是完全错误的工具,但这就是生活。 所以一直在努力开发一个网络应用程序,该应用程序基本上只是从谷歌电子表格中呈现大量信息并发回一些更新
我正在设计一个胖客户端(即非浏览器)Python 应用程序,它将与托管数据库进行通信。该应用程序的屏幕之一需要有一个网格/电子表格区域,允许显示多行数据、过滤、排序和更新多行信息。有人推荐一个具有此功
我在 Django 中有一个表单,可以将用户添加到 mySQL 数据库。然而,用户将不得不添加大量用户,并且一些信息将被复制和粘贴。所以我在想是否有一种方法可以有一个类似表单的电子表格,其中列标题是将
我在 Outlook VBA 中编写了一个程序,它根据 Excel 电子表格的内容创建一些电子邮件。一切正常,但是当程序终止时,我继续运行“EXCEL.EXE”进程,该进程会锁定电子表格,因此其他人无
如何使用公式来确定当前时区? 我使用的公式给出了意想不到的结果。 我的电子表格设置(文件 > 电子表格设置...): 时区: (GMT+01:00) 阿姆斯特丹 我使用的公式: =TEXT(NOW()
如何转换持续时间的小时数? 示例:在单元格 A1 中,我有 176 . 我希望单元格 A2 有 176:00:00 我想在 176 小时(持续时间)内转换数字 176。那可能吗?因为如果我在单元格中键
我收到了 4 个不同的采购订单,上面有一个随机的采购订单编号。 我仅在 A 列中填写采购订单号。 对于这个例子,我收到了下面列出的 4 个不同的采购订单。 采购订单 697533 - 2 项 采购订单
我需要编写循环遍历数据列的程序。根据单元格值和代表变量的每一列重置变量。 练习中的变量取决于循环的这些值。 如何在循环的每次迭代中将值增加 1 来循环遍历行? df=pd.DataFrame(r'C:
我正在做一些详尽的搜索,需要确定新域 (URL) 是否已经在电子表格中。但是,没有一个电子表格对象具有搜索功能,即在大多数文档对象中找到的 findText()。我觉得我错过了一些重要的东西。 我错过
我正在尝试在 Pandas 中导入一个 excel 电子表格,但由于数据“由用户在视觉上构建”以在视觉上对用户友好并且没有使用正确的数据结构,因此我遇到了一些问题。 结果,当在 Pandas 中作为数
我必须在 HTML 页面上显示多个图表,但我无法进行多个查询(直接在 google 电子表格上)。 实际上,我只是第一个查询显示的是谁。 我的代码: Page de pilotage
我使用以下代码作为将 html 网站上的用户输入数据发送到 Google 电子表格的方法: Javascript: function postContactToGoogle() { v
我有一个使用 Google Drive 和电子表格 API 的网站。我想要做的是,如果没有找到具有特定条件的电子表格,则在用户的 Google 云端硬盘上创建电子表格。该电子表格应该是我的 Googl
我目前链接到六个 Excel 电子表格,主要是因为用户在 Excel 中编辑数据更容易/更好(他们从未使用过 Access),而且任何更改都会立即反射(reflect),包括是否有新列添加后即可立即供
我正在努力通过 PowerShell 添加指向可变长度电子表格的链接,无论如何我都会遇到错误。这是我目前正在尝试制作的循环,它嵌套在一个 excel comobject 中, $sheet 代表 $e
在 Excel 电子表格中编写许多工作表可能需要一段时间。并行化它会很有帮助。 此代码运行良好,它使一个 Excel 电子表格在屏幕上弹出,其中包含四个名为 Sheet1、1、2 的工作表 和 3。
我是一名优秀的程序员,十分优秀!