|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
||||
|
||||
|
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! |
|
#2
|
||||
|
||||
|
Change your constructor to:
Code:
my $csv = Text::CSV->new({binary => 1 });
__________________
~ishnid; Have you tried: [ search.cpan.org | perldoc | Java API | mysql.com | google ] Apostrophes are NOT used for possessive pronouns or for noun plurals, including acronyms. |
|
#3
|
||||
|
||||
|
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....! |
|
#4
|
||||
|
||||
|
found the solution!
Yureka!
binmode OUT; # open file handle for binary Setting the binary mode for the file handle solved the problem! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Creating CSV file, colume has new lines |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|