473,396 Members | 2,004 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,396 software developers and data experts.

How to read csv file with multi line in one field

I have a test.csv file with multi line in one field like following:

Name, message
"Gus", "See you"
"Amy", "take to you later,
Thank you.
call me"

"Mark", "Try it again

Okay"


Here is my code:
Expand|Select|Wrap|Line Numbers
  1. open(CSV, "test.csv") or die "Cannot open test.csv for read\n";
  2.  my $string = <CSV>;
  3.  
  4.  while (<CSV>) 
  5.  {
  6.          chomp;
  7.          my @list = split (",");
  8.          foreach @list;
  9.          print "$list[0] <$list[1]>\n";
  10.  
  11.  }
  12.  
It read first record right, but not the second and third ones. Can someone give me some advice how to read this csv file?

Thanks a lot!!!!
May 18 '10 #1
1 4815
chaarmann
785 Expert 512MB
You shouldn't use "chomp" as your first command, or you will delete the needed newline-character inside your message.
Step 1.) get the next line
Step 2.) Count the number of double quotation marks inside this line
Step 3.) If there are four, you are done with the current record and can split by comma. Then chop and trim the double quotation marks on both ends. Process the resulting record data and go to step 1.
If there are less than four, you should not trim or chop or process. Instead, read the next line and concatenate it with the old. Then go to step 2.
If there are more than four, you should print out an error (too many columns) and exit the loop.

I am not sure if there could be a double quotation mark inside the message, probably escaped by a backslash or so. If so, then you need to count only the the double quotation marks without that slash in front.
May 19 '10 #2

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

Similar topics

3
by: stax | last post by:
Hello, can somebody tell me how to serialize/deserialize a object containing a multi line string using the XmlSerializer class. One of the both windows linefeed chars get dumped somewhere down...
3
by: Jow Blow | last post by:
I am trying to make a word wrap type function for a multi line text box field that will be saved to a text file. The word wrap property looks good in the app but when saved to a text file the line...
2
by: John Carnahan | last post by:
I have a problem writing the text from a multi-line textbox to the Sql Server db. If the user sends a vbcrlf (enter key) in the middle of the entered text, the text gets truncated at the vbcrlf...
2
by: Agnes | last post by:
Does the textbox in vb.net (alllow input multi-line ??) In vfp, there is "editbox" , which allow user to input several lines data and save in the table 'as memo field' However, I can't find the...
11
by: pmarisole | last post by:
I am trying to use the vbscript "split" function on a multi-select field. I am trying to do a mass update of several records at a time. I am getting an error and I'm not sure what to do. Here is...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
11
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const...
1
by: ashok0866 | last post by:
I had created a macro to read data from an excel sheet and write the values to a text file. I had used "ActiveSheet.Range("GB" & k).Value" command to read the values from the excel. The issue...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
3
OuTCasT
by: OuTCasT | last post by:
How does one make a field in a crystal report Multi Line ??? I have 2 fields next to each other for example : Problem Solution...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.