473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to read csv file that has ',' within data?

I am new to Perl programming and I faced a problem now.

I need to process a csv file and as for now, if the string is like '123' or 'asd', I could do it without any problem because it is commas inbetween data. But if I want to process like 'asd,asd,asd', I do not know how to read the whole string with the commas because as of now, I use split ',' to get data from different field of the csv. please help!!!
Jul 30 '08 #1
8 4670
nithinpes
410 Expert 256MB
I believe you have tried writing the code. But instead of putting it in plain words, it would have been better if you posted the piece of script that you tried and the sample data.
Suppose, your data is as below:
Expand|Select|Wrap|Line Numbers
  1. 'asd,asd,asd','123','123'
  2. 'asd','dgf','pqr'
  3.  
One way of looking at the requirement is, you would essentially need to take out the string within single quotes (' ') rather than that delimited by comma(,). You can acheive this through regex.
The code:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. open (IN,'input_file') or die "$!"; 
  3. my @data;
  4.  while (<IN>) {
  5. push @data, $1 while(/('.+?')/g); #get the string surrounded with single-quotes
  6. }
  7. print "$_\n" foreach(@data);
  8.  
will produce the output:
Expand|Select|Wrap|Line Numbers
  1. 'asd,asd,asd'
  2. '123'
  3. '123'
  4. 'asd'
  5. 'asd'
  6. 'asd'
  7.  
-Nithin
Jul 30 '08 #2
KevinADC
4,059 Expert 2GB
In a properly formatted CSV file, some of the data will be quoted and some will not be. The Text::CSV_XS module can correctly parse a CSV file with embedded commas as long as that particular field is quoted.
Jul 30 '08 #3
Thanks for the prompt reply.

My data was input through excel then save under csv. In the excel, i gt few fields... Lets say is "abc,def,ghi",123,asd

I tried this

Expand|Select|Wrap|Line Numbers
  1. my $csv = "test.csv";
  2. my @data = read_file($csv);
  3.  
  4. foreach($a=1; $a<@data.""; $a++)
  5. {
  6.     print @data[$a];
  7. }
  8.  
  9. sub read_file{
  10.     open(F, $_[0]) || die("Can't open file $_[0]!");
  11.     my @data = <F>;
  12.     close F;
  13.     return @data;
  14. }
the output is "abc,def,ghi",123,asd

What i 1 is that if i to assign "abc,def,ghi" to $name, how do i get the data within the double quote and also how to assign the 123 and asd which doesnt have the double quote as output to be $contact_no and $address respectively?

I tried using the delimited comma, but within the double quote, there are 2 commas, as a result, i will have 5 individual data...
Jul 30 '08 #4
KevinADC
4,059 Expert 2GB
I still recommend you use Text::CSV_XS but here is a way to do it without the module:

Expand|Select|Wrap|Line Numbers
  1. while (<DATA>) {
  2.     my @new  = ();
  3.     push(@new, $+) while $_ =~ m{
  4.         # the first part groups the phrase inside the quotes.
  5.         # see explanation of this pattern in MRE
  6.         "([^\"\\]*(?:\\.[^\"\\]*)*)",?
  7.            |  ([^,]+),?
  8.            | ,
  9.        }gx;
  10.        push(@new, undef) if substr($_, -1,1) eq ',';
  11.        print "$_\n" for @new;
  12. }  
  13.  
  14. __DATA__
  15. "abc,def,ghi",123,asd
The solution is from the Perl CookBook which got the regexp from "Mastering Regular Expressions". Don't ask me to explain the regexp, it confuses the hell out of me.
Jul 30 '08 #5
Thanks alot kevin.

But, if i want to use the module, do you mind if you actually show me an example? thanks alot in advance... :p
Jul 30 '08 #6
KevinADC
4,059 Expert 2GB
The modules documentation has examples. You can find the documentation on CPAN or google for Text::CSV_XS.
Jul 30 '08 #7
Just for information, may i know are you from Singapore?
Jul 30 '08 #8
KevinADC
4,059 Expert 2GB
Just for information, may i know are you from Singapore?
If you are asking me, no, I am not from Singapore. And I have never been there.
Jul 30 '08 #9

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

Similar topics

1
by: Karen Grube | last post by:
Hi! I hate to bother you all with this, but I don't know how best to approach a particular task. Here's the deal: Once a month I personally (that is, in my own personal inbox on my...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
18
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. ...
10
by: QQ | last post by:
I am trying to read data from Excel files I use the following codes, however, it didn't work Can anyone tell me what the format excel files have, what I should do to read it from C? Thanks a...
10
by: Yogi_Bear_79 | last post by:
pardon my ignorance as I am a self-taught hobbyist programmer. I am curious after reading up on SharpZipLib. Can I embed a zipped txt file in my program? Then either read from within the zip...
2
by: Karen Grube | last post by:
Hi! I hate to bother you all with this, but I don't know how best to approach a particular task. Here's the deal: Once a month I receive in my own inbox on my company's Outlook Exchange...
3
by: james.burrows | last post by:
I'm looking for a quick way to read lines of data from a text file into memory. Each line will contain e.g. a book entry: - id,name,price,category,author,description I could have over 20000 of...
4
by: Pim75 | last post by:
Hello, I have to read a XML file in ASP and save the values in a database. I can get this work, but I cannot read some nested nodes of the xml file. This is a part of the XML file: ...
0
by: drharris | last post by:
First, please forgive my newness to XML. I've used it to serialize/ deserialize objects, exporting and importing datasets, and other such things that pretty much automate reading in the file. I've...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.