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 ()