Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here's an excerpt from the documentation of Text::CSV_XS:

The separation-, escape- [sic], and escape- characters can be any ASCII character in the range from 0x20 (space) to 0x7E (tilde). Characters outside this range may or may not work as expected. Multibyte characters, like U+060c (ARABIC COMMA), U+FF0C (FULLWIDTH COMMA), U+241B (SYMBOL FOR ESCAPE), U+2424 (SYMBOL FOR NEWLINE), U+FF02 (FULLWIDTH QUOTATION MARK), and U+201C (LEFT DOUBLE QUOTATION MARK) (to give some examples of what might look promising) are therefor [sic] not allowed.
If you use perl-5.8.2 or higher, these three attributes are utf8-decoded, to increase the likelihood of success. This way U+00FE will be allowed as a quote character.

Is it possible to use \x{fe} for the quote_char character? If not, why not? Why is the Unicode code point U+00FE specifically mentioned in the documentation?

Here's a script I used to test using \x{14} for the sep_char character and \x{fe} for the quote_char character:

#!perl use strict; use warnings; use Text::CSV_XS; my $dat_file = <<EOF; \x{feff}\x{fe}Row1Col1\x{fe}\x{14}\x{fe}Row1Col2\x{fe}\x{14}\x{fe}Row1 +Col3\x{fe} Row2Col1\x{14}Row2Col2\x{14}Row2Col3 Row3Col1\x{14}\x{fe}Row3Col2\x{fe}\x{14}\x{fe}R\x{14}ow3Col3\x{fe} \x{fe}Row4Col1\x{fe}\x{14}Row4Col2\x{14}\x{fe}Row4C\x{fe}\x{fe}ol3\x{f +e} Row5Col1\x{14}Row5Col2\x{14}\x{fe}Row5\nCol3\x{fe} EOF my $dat = Text::CSV_XS->new({ sep_char => "\x{14}", quote_char => "\x{fe}", escape_char => "\x{fe}", eol => $/, binary => 1, keep_meta_info => 1, auto_diag => 1, }); open my $dat_fh, '<:encoding(UTF-8)', \$dat_file; binmode STDOUT, ':encoding(UTF-8)'; while (my $row = $dat->getline($dat_fh)) { my $r = $.; for my $c (1 .. @$row) { my $is_quoted = $dat->is_quoted($c - 1) ? 'Is Quoted' : 'Is Not Quoted'; print "Row $r, Col $c $is_quoted: $row->[$c - 1]\n"; } } $dat->eof() or $dat->error_diag(); close $dat_fh; exit 0;

Here's the output of the script:

Row 1, Col 1 Is Not Quoted:  þRow1Col1þ
Row 1, Col 2 Is Not Quoted:  þRow1Col2þ
Row 1, Col 3 Is Not Quoted:  þRow1Col3þ
Row 2, Col 1 Is Not Quoted:  Row2Col1
Row 2, Col 2 Is Not Quoted:  Row2Col2
Row 2, Col 3 Is Not Quoted:  Row2Col3
Row 3, Col 1 Is Not Quoted:  Row3Col1
Row 3, Col 2 Is Not Quoted:  þRow3Col2þ
Row 3, Col 3 Is Not Quoted:  þR
Row 3, Col 4 Is Not Quoted:  ow3Col3þ
Row 4, Col 1 Is Not Quoted:  þRow4Col1þ
Row 4, Col 2 Is Not Quoted:  Row4Col2
Row 4, Col 3 Is Not Quoted:  þRow4Cþþol3þ
Row 5, Col 1 Is Not Quoted:  Row5Col1
Row 5, Col 2 Is Not Quoted:  Row5Col2
Row 5, Col 3 Is Not Quoted:  þRow5
Row 6, Col 1 Is Not Quoted:  Col3þ

Obviously, the \x{fe} characters aren't being recognized as quote and escape characters.

(I'm running Perl 5.14.2 and Text::CSV_XS 0.90.)

Jim


In reply to Non-ASCII Characters As Separators/Quotes/Escapes When Using Text::CSV_XS by Jim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found