473,772 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read csv file with multi line in one field

2 New Member
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 4854
chaarmann
785 Recognized Expert Contributor
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
10412
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 the road. It would be awesome somebody could answer this, sadly most of my questions don't get answered. Regards,
3
4016
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 goes on and on where there is not a new line character in the text box. Here's kinda what I'm looking at: //start snippet public void WrapIt() {
2
2676
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 when writting the data to the database. The text could get rather long. The dataset has all of the text (including the vbcrlf), but the resulting table field in the database only ends up with the portion of text before the
2
2362
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 similar thing in vb.net and sql server (ntext ?) please help Thanks -- ..
11
4440
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 the code if someone could help... strID = split(request.form("proj"), ", ") projstat = split(request.form("rojstat"),",") impr = split(request.form("impr"),",") idate =...
23
5335
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 I applied myself to this problem and come up with this off-the-wall proposal for you people. Perhaps this idea has been proposed before, I don't know. The solutions I have seen all assume that the lambda must be completely inlined within the...
11
6253
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 string multiline = "The first line." + Environment.NewLine +
1
22193
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 is: some cells in the excel sheet is having two lines value and four lines data, the text file is generating that value in a single line. (Ex. the cell value in address column is in four lines and should display in four lines in the text file...
1
3380
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 micro-controllers. So why am I posting herein? Well, the ARMbasic environment makes use of a tool borrowed from your folk's environment - CPP. The build details are: C:\Program Files\Coridium>cpp --version cpp (GCC) 3.2.3 (mingw special 20030504-1) Copyright...
3
4924
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 ------------------------------------------------------ The problem is that the problem field text might get too long and it will then overlap the solutions text. I have the report on landscape for more space but i sometimes get the same problem....
0
10104
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.