python - txt text processing

1. Remove blank line in the text, the text written in the other

# -*- coding:utf-8 -*-
 
f=open('data.txt')
g=open('data5.txt','w')
 
try:
    while True:
        line=f.readline()
        if len(line)==0:
            break
        if line.count('\n')==len(line):
            continue
        g.write(line)
finally:
    f.close()
    g.close()

2. Find the line with a keyword from the text

import os

f1=open('data.txt','r')   #保存的txt
f2=open('data2.txt','w+')   #保存的txt
txt=f1.readlines()
n=0
for line in txt:
	n = n + 1
	if "High" in line:
		line = line.strip()
		f2.write(txt[n]+'\n')
		print(n,line)
		print(txt[n])


3. Find the number of times each line of text that appears in

#_*_coding:utf-8_*_
import operator
f = open("data2.txt")
f2=open("gg.txt","w+")
# 初始化空字典,key为行内容,value为该行出现的次数
result = {}
for line in f:
    line = line.strip()
    count = result.setdefault(line,0)
    count += 1
    result[line] = count
sorted_result = sorted(result.items(), key=operator.itemgetter(1),reverse=True)
for k,v in sorted_result:
    f2.write(k+"---"+str(v)+'\n')
    print (k,"---",v)

Txt 4. All the directories in the merged

import glob
import os
 
path='J:/txt/v20/'   #遍历的路径
f1=open('J:/v20.txt','w+')   #保存的txt

for root,dirs,files in os.walk(path):
	for file in files:
		folder=root.split('/')[-1]   #取得txt对应的文件夹名称
		print(folder)

		if file.endswith('txt'):
			print('find it---',root+'/'+file)
			txt_path=root+'/'+file   #打开找到的txt
			for line in open(txt_path):
				f1.write(folder+'     '+line)   #读行,写入新txt中
f1.close()

end.

Guess you like

Origin blog.csdn.net/gm_Ergou/article/details/92840713