Parse :: CSV,解析没有标题的文件

时间:2018-04-30 16:29:34

标签: perl csv parsing

我给出的数据是来自mysql数据库的转储,有些文件缺少列标题。

当我尝试获取一行时, 说Dumper $ parser-> fetch; 会在没有列标题的文件上返回undefined。我做了一个if(-e $文件),它看到了文件。我也试过了

names => 1

names => ['id','description']

并且两次输出都是undef,但后者$ parser->名称输出 id description (名称,而不是列值)。

我是否正确分配了列名?如果是这样,为什么它返回undefined?

#!/usr/bin/perl

use v5.24.3;
use strict;
use warnings;

# Modules
use Data::Dump;                                     # dd \%hash
use Data::Dumper;                                   # say Dumper(\%hash)                        
use Spreadsheet::ParseXLSX;
use Text::CSV_XS;
use Parse::CSV;

my $path    = './path/to/data/';
my $vendor  = 'fwd';
my $ext     = '.csv';
my @f       = (
    'spring_categories',
    'prod_descriptions',
    'feature_bullets',
    'category_mapping'
);
my $file = $path . $vendor . '/' . $f[1] . $ext;

my $parser = Parse::CSV->new(
    "file"        => $file,                                       
    "names"       => ['id', 'description'],
    "csv_attr"    => {
        "sep_char" => ',',
        "quote_char" => "'"
    }
);

# Not triggering error
if ( $parser->errstr )
{
    say "There was an error";
}
else
{
    say $parser->names;             # outputs | iddescription
    say Dumper $parser->fetch;      # outputs | $VAR1 = undef

    # Doesn't reach while
    while ( my $value = $parser->fetch )
    {
        say Dumper $value;
        say $parser->row;

        # my @names = $parser->names;
        # dd \@names;
        # say $names[0];

        die;
    }

这是我在没有机密数据的情况下共享的临时测试文件,对我来说存在同样的问题。

4732947234,"Lorem ipsum dolor sit amet, consectetur adipiscing elit"
6732947274,"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo"
7657657274,Nemo enim ipsam voluptatem quia voluptas 
3993007274,magnam aliquam quaerat voluptatem
2449049474,"laboriosam, nisi ut aliquid ex ea commodi consequatur"
4732947273,"laboriosam, nisi ut aliquid ex ea commodi consequatur"
8732947270,"ntium doloremque laudantium, totam rem aperiam, eaque ipsa "

2 个答案:

答案 0 :(得分:1)

您的构造函数指定quote_char => "'",而您的示例数据文件使用双引号"

如果CSV数据中没有标题,那么您应该使用names => 0让模块返回数据数组,而不是按列名称键入哈希

我还建议您明确open文件并检查它是否成功。然后,您可以在file参数中传递您知道的有效文件句柄而不是路径字符串

答案 1 :(得分:-1)

下面的代码解析你提出的test.csv文件:

import React, { Component } from 'react';

class Testing extends Component {
    state = {
        arrayCodes: ['A1', 'A2'],
        currentCode: '',
    }

    addEditCode = (inputCode) => {
        //console.log("Add Edit Code")
        var arrayCode;
        arrayCode = this.state.arrayCodes
        console.log("array code before push", arrayCode)
        arrayCode.push(inputCode)
        console.log("array code after push", arrayCode)
        this.setState({ arrayCodes: arrayCode })
        console.log("Array of Codes is now: ", this.state.arrayCodes)
    }

    setCurrentCode = (input) => {
        this.setState({ currentCode: input })
    }

    render() {
        return (
            <div>
                <input type="text"
                    name="enteredCode"
                    placeholder="Enter an edit code to add..."
                    onChange={(event) =>         
                    this.setCurrentCode(event.target.value)} />

                <button onClick={() => 
                this.addEditCode(this.state.currentCode)}>Click to 
                add</button>

                <h1>Current array in state: {this.state.arrayCodes}</h1>

            </div>
        );
     }
}

export default Testing;