Remove the first line number from Python

Source: Internet
Author: User
Sometimes, Code It is often a document with a line number. At this time, we need to remove the line number from the beginning of the line to run in our document. The following code is to remove the line first code module, python implementation:

# ! /Usr/bin/ENV Python
# Coding = UTF-8

# Remove line number before copy code from other
# File

Class Parser ():
"""  
Parse the file, and remove the line number
Of given text, and remove the left and right space
"""
Def   _ Init __ (Self, raw ):
Self. Raw = Raw. splitlines ()
Self. Result = []

Def Remove (Self ):
# Find lines of the file
# Lines = Len (self. Raw)
Number = 1
For Line In Self. Raw:
Line = Line. lstrip (). rstrip (). Replace (STR (number ), '' )
Self. Result. append (line)
Number + = 1
Return   " \ N " . Join (self. Result)

Def Test ():
Testlines =   " 1 print 'Hello world' \ n \
2   If   _ Name   =   ' Main ' : \ N \
3 Print   ' Third Line ' \ N \
"
Print Parser (testlines). Remove ()

If _ Name __ = "_ Main __":
Test ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.