Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 25th, 2008, 07:06 AM
bhagwatn's Avatar
bhagwatn bhagwatn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: India
Posts: 163 bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 43 m 7 sec
Reputation Power: 15
Creating CSV file, colume has new lines

Can someone tell me how to create a CSV file where a column contains new lines as well.

When I did this manually, I placed '\n' manually, but it gets shown as some weird control character in Excel.

Tried to use the Text::CSV module and it fails to create a line having a column with new-lines.

Following is the failing code:
Code:
use Text::CSV;
my @columns = ("Col1", "Col2", "line1\nline2\nline3");
$csv = Text::CSV->new();              # create a new object
$status = $csv->combine(@columns);    # combine columns into a string
# following gives a empty $line!
$line = $csv->string();               # get the combined string
open(OUT, ">t.csv");
print OUT "$line\n";
close OUT;

If you remove newline from the @columns, it works fine!

Reply With Quote
  #2  
Old April 25th, 2008, 10:35 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God (5000 - 5499 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,394 ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level)ishnid User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 5 Days 14 h 14 m 24 sec
Reputation Power: 1259
Change your constructor to:
Code:
my $csv = Text::CSV->new({binary => 1 });

Reply With Quote
  #3  
Old May 2nd, 2008, 01:29 AM
bhagwatn's Avatar
bhagwatn bhagwatn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: India
Posts: 163 bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 43 m 7 sec
Reputation Power: 15
even binary option shows the same problem

Ishnid,
even with binary flag set to true, it still shows the same control character when opened in excel. See that attached attachment to see what the problem is.

I even tried to create similar file via excel (having multiple lines in a cell via ALT+ENTER, saved this file as .csv) and read this file character by character. For both files, it shows ASCII 10 in place of the new line.

Help....!
Attached Images
File Type: jpg csv_problem.JPG (12.6 KB, 10 views)
Attached Files
File Type: zip t.zip (130 Bytes, 8 views)

Reply With Quote
  #4  
Old May 2nd, 2008, 01:59 AM
bhagwatn's Avatar
bhagwatn bhagwatn is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: India
Posts: 163 bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level)bhagwatn User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 43 m 7 sec
Reputation Power: 15
found the solution!

Yureka!
binmode OUT; # open file handle for binary

Setting the binary mode for the file handle solved the problem!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Creating CSV file, colume has new lines


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway