467,882 Members | 1,249 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,882 developers. It's quick & easy.

How to substitute the space for field of csv format?

Currently my data is this in excel but as a csv format:

"a", "as " sd", "123"
"b","asd","234"
"c","as d","345"

I wanted to make the output this:
a,as " sd,123
b,asd,234
c,as d,345

the code i tried is this

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Text::CSV_XS;
  3.  
  4. my $csv = Text::CSV_XS->new({'allow_whitespace'=>1,'allow_loose_quotes'=>1});
  5. my $file = "test.csv";
  6.  
  7. open(DAT, $file) || die("Cannot Open File");
  8.  
  9. while (<DAT>) {
  10.     $csv->parse($_);
  11.     my @fields = $csv->fields();
  12.     foreach ("@fields")
  13.     {
  14.         $_ =~ s/ +/,/;
  15.         print "$_\n";
  16.     }
  17. }
  18.  
How can i modify to get what I desired?
Aug 14 '08 #1
  • viewed: 1176
Share:
1 Reply
In your code at line number 14. You need to use \s+ for one or more space character.
so it will be
Expand|Select|Wrap|Line Numbers
  1. $_ =~ s/\s+/,/;
Hope this works
Aug 15 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

35 posts views Thread by Felix Kater | last post: by
5 posts views Thread by Stu Carter | last post: by
2 posts views Thread by empiresolutions | last post: by
4 posts views Thread by michael.boucher | last post: by
10 posts views Thread by Phil Stanton | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.