Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:34:14+00:00 2026-06-17T16:34:14+00:00

i’m working on a file i have scraped from a website, the file is

  • 0

i’m working on a file i have scraped from a website, the file is saved as a semicolon csv with quoted fields.
The last field contains embedded newlines.
I’ve been working on a script to proces the file.
I’m fairly new to perl and at first is was trying it with a normal perl script but quickly found out that wasn’t working.
I did my research and found out I should use the Text::CSV module instead. I came across these sites which explained how to use the module:

http://perlmaven.com/how-to-read-a-csv-file-using-perl

http://perlmeme.org/tutorials/parsing_csv.html

http://metacpan.org/pod/Text::CSV#Embedded-newlines

Basically what i’m trying to accomplish is to read the file correctly so that all the fields get delimited properly instead of breaking off at a newline. Then removing the newlines from that field and write it to a new file.

Here is an example of the original data:

 "2030";"NH Amersfoort";"Stationsstraat 75";"3811 MH AMERSFOORT";"033-4221200";"www.nh-hotels.nl";"52.154316";"5.380036";"<UL class=stars><LI>
 <LI>
 <LI>
 <LI></LI></UL>"
 "2031";"NH Amsterdam Centre";"Stadhouderskade 7";"1054 ES AMSTERDAM";"020-6851351";"www.nh-hotels.com";"52.363075";"4.879458";"<UL class=stars><LI>
 <LI>
 <LI>
 <LI></LI></UL>"
 "2032";"NH Atlanta Rotterdam Hotel";"Aert van Nesstraat 4";"3012 CA ROTTERDAM";"010-2067800";"www.nh-hotels.com";"51.921028";"4.478619";"<UL class=stars><LI>
 <LI>
 <LI>
 <LI></LI></UL>" 

And what i want is this:

 "2030";"NH Amersfoort";"Stationsstraat 75";"3811 MH AMERSFOORT";"033-4221200";"www.nh-hotels.nl";"52.154316";"5.380036";"<UL class=stars><LI><LI><LI><LI></LI></UL>"
 "2031";"NH Amsterdam Centre";"Stadhouderskade 7";"1054 ES AMSTERDAM";"020-6851351";"www.nh-hotels.com";"52.363075";"4.879458";"<UL class=stars><LI><LI><LI><LI></LI></UL>"
 "2032";"NH Atlanta Rotterdam Hotel";"Aert van Nesstraat 4";"3012 CA ROTTERDAM";"010-2067800";"www.nh-hotels.com";"51.921028";"4.478619";"<UL class=stars><LI><LI><LI><LI></LI></UL>" 

This is my full script so far. I have tried 10 different options and suggestions and they’re all not working!

 use strict;
 use warnings;    
 use Text::CSV;

 my $inputfile  = shift || die "Give input and output names!\n";
 my $outputfile = shift || die "Give output name!\n";

 open my $infile,  '<', $inputfile   or die "Sourcefile in use / not found :$!\n";
 open my $outfile, '>', $outputfile  or die "Outputfile in use :$!\n";

    my $csv = Text::CSV->new ({
binary => 1,
sep_char => ';'
});

while (my $elements = $csv->getline( $infile )) {
        my $stars = $elements->[8];
        #$ster =~ s/[\r\n]//g
        print "$stars\n\n";
        }

 close $infile;
 close $outfile;

This prints the field with the newlines in it correctly but hasn’t removed them off course. How do i do that? Using a regex to substitute the newlines is not working. And the next question is when I do figure out how to clean up that field.. How do i print the new file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:34:15+00:00Added an answer on June 17, 2026 at 4:34 pm

    I’m not sure what you are asking here, because it seems you already have your answers. However, this code does work:

    use strict;
    use warnings;
    use Text::CSV;
    
    my $csv = Text::CSV->new ({
        binary => 1,
        sep_char => ';',
        eol => $/,                # to make $csv->print use newlines
        always_quote => 1,        # to keep your numbers quoted
    });
    
    while (my $row = $csv->getline( *DATA )) {
        $row->[8] =~ s/[\r\n]+//g;
        $csv->print(*STDOUT, $row);
    }
    
    __DATA__
    "2030";"NH Amersfoort";"Stationsstraat 75";"3811 MH AMERSFOORT";"033-4221200";"www.nh-hotels.nl";"52.154316";"5.380036";"<UL class=stars><LI>
    <LI>
    <LI>
    <LI></LI></UL>"
    "2031";"NH Amsterdam Centre";"Stadhouderskade 7";"1054 ES AMSTERDAM";"020-6851351";"www.nh-hotels.com";"52.363075";"4.879458";"<UL class=stars><LI>
    <LI>
    <LI>
    <LI></LI></UL>"
    "2032";"NH Atlanta Rotterdam Hotel";"Aert van Nesstraat 4";"3012 CA ROTTERDAM";"010-2067800";"www.nh-hotels.com";"51.921028";"4.478619";"<UL class=stars><LI>
    <LI>
    <LI>
    <LI></LI></UL>"
    

    Pointers:

    Using the eol option with Text::CSV‘s print makes it do what you expect, which is to print newlines. I used STDOUT as the output handle, but you can use any file handle you want.

    I don’t know why you say substitution does “not work” for you, but I suspect that perhaps you did something like this:

    my $foo = $row->[8];
    $foo =~ s/[\r\n]//g;
    print @$row;
    

    This does not change the values in $row, just the copy in $foo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.