what is trailing whitespace and how can I handle this?

前端 未结 4 490
抹茶落季
抹茶落季 2021-02-06 20:37

some piece of my codes:

            if self.tagname and self.tagname2 in list1:
                try: 
                    question = soup.find(\"div\", \"post-te         


        
4条回答
  •  野的像风
    2021-02-06 20:48

    Trailing whitespace:

    It is extra spaces (and tabs) at the end of line      
                                                     ^^^^^ here
    

    Strip them:

    #!/usr/bin/env python2
    """\
    strip trailing whitespace from file
    usage: stripspace.py 
    """
    
    import sys
    
    if len(sys.argv[1:]) != 1:
      sys.exit(__doc__)
    
    content = ''
    outsize = 0
    inp = outp = sys.argv[1]
    with open(inp, 'rb') as infile:
      content = infile.read()
    with open(outp, 'wb') as output:
      for line in content.splitlines():
        newline = line.rstrip(" \t")
        outsize += len(newline) + 1
        output.write(newline + '\n')
    
    print("Done. Stripped %s bytes." % (len(content)-outsize))
    

    https://gist.github.com/techtonik/c86f0ea6a86ed3f38893

提交回复
热议问题