英文:
PHP script creates XML files from CSV's but holds 0 contents
问题
我已经创建了一个PHP脚本,用于读取19个CSV文件并规范化数据 - 不保存重复数据(站点ID、名称和地理编码),只保存每个记录中具有值的属性。我已经打印了CSV文件,以确保它们在脚本中加载,但是脚本生成的data-site-id文件内容为空?
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// CSV文件列表
$csvFiles = [
'data-203.csv',
'data-206.csv',
'data-209.csv',
'data-270.csv',
'data-213.csv',
'data-215.csv',
'data-271.csv',
'data-375.csv',
'data-395.csv',
'data-447.csv',
'data-452.csv',
'data-463.csv',
'data-500.csv',
'data-501.csv',
'data-672.csv',
'data-188.csv',
'data-209.csv',
'data-481.csv',
'data-228.csv',
'data-459.csv',
];
// 遍历每个CSV文件
foreach ($csvFiles as $csvFile) {
$csvFilePath = __DIR__ . '/' . $csvFile;
// 打开CSV文件
if (($handle = fopen($csvFilePath, 'r')) !== false) {
// 创建XMLWriter实例
$xmlWriter = new XMLWriter();
$xmlWriter->openURI(csvFileToXmlFile($csvFile));
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->startElement('data');
// 逐行读取CSV文件
while (($data = fgetcsv($handle)) !== false) {
// 跳过空行
if (count($data) === 0) {
continue;
}
// 确保数组具有必要的索引
if (count($data) >= 4) {
// 提取必要的数据
$stationId = $data[0];
$name = $data[1];
$geocode = $data[2];
$attribute = $data[3];
// 检查属性是否具有值
if (trim($attribute) !== '') {
// 开始XML记录元素
$xmlWriter->startElement('record');
$xmlWriter->writeAttribute('station_id', $stationId);
$xmlWriter->writeAttribute('name', $name);
$xmlWriter->writeAttribute('geocode', $geocode);
$xmlWriter->writeAttribute('attribute', $attribute);
// 结束XML记录元素
$xmlWriter->endElement();
}
}
}
// 关闭XML元素和文件
$xmlWriter->endElement();
$xmlWriter->endDocument();
$xmlWriter->flush();
fclose($handle);
// 输出成功消息
$xmlFile = csvFileToXmlFile($csvFile);
echo "XML文件成功生成:$xmlFile\n";
}
}
// 将CSV文件名转换为XML文件名的函数
function csvFileToXmlFile($csvFile) {
$fileParts = pathinfo($csvFile);
$xmlFile = $fileParts['filename'] . '.xml';
return $xmlFile;
}
?>
正如提到的,我已经打印了CSV文件以确保它们在目录中被读取。我还将 $xmlWriter->openURI($csvFileToXmlFile($csvFile));
更改为 $xmlWriter->openURI(csvFileToXmlFile($csvFile));
。之前在52和53行出现的Undefined offset 2和3的错误也已经消失。
CSV的示例图像,不确定如何发送:
查看图像
空白的XML文件 - data-481.xml:
查看XML
英文:
I have created a php script to read through 19 csv files and normalises the data - not holding repeating data (station id, name & geocode) and only holding the attributes for each record which have values. I have printed the csv's to make sure the are loading within the script and the script also makes the data-site-id files but contains 0 contents within the file?
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// List of CSV files
$csvFiles = [
'data-203.csv',
'data-206.csv',
'data-209.csv',
'data-270.csv',
'data-213.csv',
'data-215.csv',
'data-271.csv',
'data-375.csv',
'data-395.csv',
'data-447.csv',
'data-452.csv',
'data-463.csv',
'data-500.csv',
'data-501.csv',
'data-672.csv',
'data-188.csv',
'data-209.csv',
'data-481.csv',
'data-228.csv',
'data-459.csv',
];
// Loop through each CSV file
foreach ($csvFiles as $csvFile) {
$csvFilePath = __DIR__ . '/' . $csvFile;
// Open the CSV file
if (($handle = fopen($csvFilePath, 'r')) !== false) {
// Create an XMLWriter instance
$xmlWriter = new XMLWriter();
$xmlWriter->openURI(csvFileToXmlFile($csvFile));
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->startElement('data');
// Read the CSV file line by line
while (($data = fgetcsv($handle)) !== false) {
// Skip empty lines
if (count($data) === 0) {
continue;
}
// Ensure the array has the necessary indexes
if (count($data) >= 4) {
// Extract the necessary data
$stationId = $data[0];
$name = $data[1];
$geocode = $data[2];
$attribute = $data[3];
// Check if the attribute has a value
if (trim($attribute) !== '') {
// Start XML record element
$xmlWriter->startElement('record');
$xmlWriter->writeAttribute('station_id', $stationId);
$xmlWriter->writeAttribute('name', $name);
$xmlWriter->writeAttribute('geocode', $geocode);
$xmlWriter->writeAttribute('attribute', $attribute);
// End XML record element
$xmlWriter->endElement();
}
}
}
// Close XML elements and file
$xmlWriter->endElement();
$xmlWriter->endDocument();
$xmlWriter->flush();
fclose($handle);
// Output success message
$xmlFile = csvFileToXmlFile($csvFile);
echo "XML file generated successfully: $xmlFile\n";
}
}
// Function to convert CSV file name to XML file name
function csvFileToXmlFile($csvFile) {
$fileParts = pathinfo($csvFile);
$xmlFile = $fileParts['filename'] . '.xml';
return $xmlFile;
}
?>
As mentioned, I have printed the csv's to make sure they are being read within the directory. I also changed the $xmlWriter->openURI($csvFileToXmlFile($csvFile)); to $xmlWriter->openURI(csvFileToXmlFile($csvFile));. I have been getting Undefined offset 2 & 3. Line 52 & 53 error before hand too, these have subsided.
Sample image of csv, wasn't sure how to send it
https://drive.google.com/file/d/1EUHN4RCJFq-XrIBQNvZphh7vxZDdbf_R/view?usp=share_link
Blank XML - data-481.xml
https://drive.google.com/file/d/1pEinOeosPQQRoeQoJHfEb6L_80iD5bqr/view?usp=share_link
答案1
得分: 1
你必须学会仔细查看你的数据。csv
文件实际上是 ssv
文件,即"分号分隔值"文件。
你只需更改代码中的这行来让 fgetcsv()
知道分隔符是 ;
:
while (($data = fgetcsv($handle, 10000, ';')) !== false) {
数据文件 "data-481.csv" 的第二个问题是,对于所有行,$data[1]
、$data[2]
和 $data[3]
列都为空。
因此,以下代码:
$attribute = $data[3];
if (trim($attribute) !== '') {
用于控制是否将该行写入 XML 文件失败,因此没有数据被写入。
英文:
You must learn to look closely at your data. The csv
files are in fact ssv
files "SemiColon seperated value" files
All you need to do is change this line of code to let fgetcsv()
know the seperator is a ;
while (($data = fgetcsv($handle,10000,';')) !== false) {
Second Issue with data-481.csv
the $data[1], $data[2], $data[3]
columns are all empty for all lines
So
$attribute = $data[3];
if (trim($attribute) !== '') {
which controls whether the row gets written to the xml file fails, so no data is written
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论