1:www.jb51.net 2:www.jb51.net 3:www.jb51.net test code 4:www.jb51.net 5:www.jb51.net 6:www.jb51.net 7:www.jb51.net 8:www.jb51.net 9:www.jb51.net 10:www.jb51.net 11:www.jb51.net 12:www.jb51.net 13:www.jb51.net 14:www.jb51.net 15: www.jb51.net 16:www.jb51.net



code # -*- coding: UTF-8 -*- 'Python read the file, the file in the blank line' def delblankline (infile, outfile): infopen = (infile,'r', open encoding= "UTF-8") outfopen = open (outfile,'w', encoding=, UTF-8) lines = infopen.readlines (for) line in lines: if (line.split): outfopen.writelines (line) else: outfopen.writelines ("infopen.close") (outfopen.close) (delblankline) ("jb51.txt" The "o.txt"),

 coding: code two # -*- UTF-8 -*- 'Python read the file, the file in the blank line' def delblankline (infile, outfile): infopen = open (infile,'r', encoding=, UTF-8) outfopen = open (outfile,'w', encoding= "UTF-8" lines) infopen.readlines line in (for) = lines: line = line.strip (if) LEN (line) =0:! Outfopen.writelines (line) outfopen.write ('n') infopen.close (outfopen.close) (delblankline) ("jb51.txt", "o2.txt") 


 #coding:utf-8 import three code: sys def delete (filepath): f=open (filepath'a+') fnew=open (filepath+'_new.txt','wb') # will result in a new text in for line in (f.readlines): # on each line to remove the space, n and other useless characters Then, check whether the trip length is 0 data=line.strip (if) LEN (data) =0:! Fnew.write (data) fnew.write ('n') f.close (fnew.close) __name__=='__main__' (if): if len (sys.argv) ==1: print U "must enter the file path, it is best not to use Chinese path" else: delete (sys.argv[1]) 


1. Python split code analysis: (a) by specifying the delimiter slicing strings, string returns a list after segmentation. The str.split () separators are default spaces.

2. function writelines (list)

function writelines can write list to file, but not add list line to every element of list, so if you want to have line breaks on every line, you need to add it yourself.

, for example, for line in lines:

outfopen.writelines (line+ n),

n (), automatically analyzes file content into a list of rows, which can be processed by the structure of the structure.

This concludes the body part

This paper fixed link:http://www.script-home.com/python-removes-a-variety-of-implementations-of-the-blank-line.html | Script Home | +Copy Link

Article reprint please specify:Python removes a variety of implementations of the blank line | Script Home

You may also be interested in these articles!