我如何在Python 3中使用EOF

时间:2018-08-13 06:55:14

标签: python python-3.x eof

在c / c ++中,我像

一样使用EOF
my @txts = data();
dd @txts;
# this works too
my %stuff = hashdata();
dd %stuff;

# a lot of lines

sub data() {
    return ( q:to/LINE1/,
        Phasellus dictum, nunc id vestibulum rhoncus, mauris massa tempus nibh, 
        nec tincidunt nisi tellus et arcu. Phasellus vulputate consectetur
        vulputate. Quisque viverra commodo velit ac tincidunt. Nulla et est sem.
        Mauris gravida, nulla rutrum pharetra dapibus, eros velit feugiat nibh,
        nec iaculis purus urna ut diam. Praesent molestie felis a turpis gravida
        placerat. Duis sagittis pulvinar risus non aliquet. Nunc quis purus
        tempor, mattis nunc eu, porta ligula. Suspendisse dictum sit amet urna
        dapibus suscipit.  
        LINE1  
        q:to/LINE2/,  
        Praesent molestie felis a turpis gravida
        placerat. Duis sagittis pulvinar risus non aliquet. Nunc quis purus
        tempor, mattis nunc eu, porta ligula. Suspendisse dictum sit amet urna
        dapibus suscipit.
        LINE2
        q:to/LINE3/);
        Quisque viverra commodo velit ac tincidunt. Nulla et est sem.
        Mauris gravida, nulla rutrum pharetra dapibus, eros velit feugiat nibh,
        nec iaculis purus urna ut diam. Praesent molestie felis a turpis gravida
        placerat.
        LINE3
}

sub hashdata() { # a hash works too.
      return ( 'p' => q:to/PDATA/,
        Some multiline data
        in some lines
        PDATA

        'q' => q:to/QDATA/,
           More data in 
           multiple lines
           QDATA

         'r' => q:to/RDATA/
              Note that indentation depends on the position of the 
              ending token.
              Also, the punctuation following the regex is the punctuation
              following the expression. So a comma after each of the 
              p and q, but not needed after the r
              RDATA
         )
}

现在如何在Python中使用EOF?

请使用python给我相同的代码

1 个答案:

答案 0 :(得分:1)

您将在python中执行以下操作:

with open(filename, 'r') as f:
    for line in f:
        print(line)