zoukankan      html  css  js  c++  java
  • python去除行首行号

    有时候,从网络文档中下载的代码,往往是带有行号的文档,这个时候,我们需要将行首的行号去除,才能够在我们的文档中运行,下面的代码是为去除行首代码模块,python实现:

    #!/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()

  • 相关阅读:
    phpcms后台进入地址(包含No permission resources错误)
    phpmyadmin上传大sql文件办法
    ubuntu彻底卸载mysql
    Hdoj 2602.Bone Collector 题解
    一篇看懂词向量
    Hdoj 1905.Pseudoprime numbers 题解
    The Python Challenge 谜题全解(持续更新)
    Hdoj 2289.Cup 题解
    Hdoj 2899.Strange fuction 题解
    Hdoj 2199.Can you solve this equation? 题解
  • 原文地址:https://www.cnblogs.com/ubunoon/p/1337905.html
Copyright © 2011-2022 走看看