移动对某个txt的信息处理,信息举例如下:
输入:
栖霞基站—侯丰明:
栖霞累计保障人数6人,保障车辆3辆,发电站1次,油机1,发电车0。
招远徐绍东:
招远阴天,当前保障人数8人,保障车辆4辆,发电站0次,油机0,发电车0。
雪中茗:
长岛阴天,当前保障人数4人,保障车辆1辆,发电站0次,油机0,发电车0。
高新驻点—于海龙:
莱山小雨,当前保障人数8人,保障车辆4辆,发电站0次,油机0,发电车0。
高新驻点—于海龙:
高新小雨,当前保障人数4人,保障车辆2辆,发电站0次,油机0,发电车0。
福山~王建:
福山小雨,当前保障人数6人,保障车辆3辆,发电站0次,油机1,发电车0。
王兴虎:
开发区小雨,当前保障人数8人,保障车辆4辆,发电站0次,油机0,发电车0。
输出:
遵循特点:
1.上述信息存放在txt文本文件中,需要导出的数据和天气情况存放在xls表格中
2.保障人数、保障车辆、发电站、油机、发电车的顺序一定,对上述5者的数量进行全自动统计
3.保证前四个中文字符,符合格式:前2字区县,后两字天气 或者 前3字区县,后两字天气
代码:start.py
import re
from openpyxl import Workbook
from openpyxl.workbook import Workbook
from openpyxl.reader.excel import load_workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
import openpyxl
import xlwt
string = "保障车辆4辆。"
#num=[[0 for i in range(n)] for j in range(m)]
#weather=["小雨","中雨","大雨","暴雨","冰雹","阴天","晴天","小雪","中雪","大雪"]
cell_overwrite_ok=True
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('My Worksheet',cell_overwrite_ok=True)
#excel_content=workbook.add_sheet('My Worksheet')
with open("C:/Users/w1248/Desktop/pyreadline.txt","r",encoding='UTF-8') as f:
lines = f.readlines()
ii=0
for line in lines:
print(line)
char1=line.encode("UTF-8").decode("UTF-8")
char1=line[0:2]
if(char1=="栖霞" or char1=="牟平" or char1=="海阳" or char1=="蓬莱" or char1=="龙口" or char1=="长岛" or char1=="莱山" or char1=="莱阳" or char1=="福山" or char1=="莱州" or char1=="招远" or char1=="芝罘" or char1=="高新"):
bools=1
char1 = line[0:2]
elif(char1=="开发"):
bools=0
char1 = line[0:3]
print(char1)
char2 = line.encode("UTF-8").decode("UTF-8")
char2 = line[2:4]
if(bools==1 and (char2=="小雨" or char2=="中雨" or char2=="大雨" or char2=="暴雨" or char2=="冰雹" or char2=="阴天" or char2=="晴天" or char2=="小雪" or char2=="中雪" or char2=="大雪")):
char2 = line[2:4]
elif(bools==0 and (line[3:5]=="小雨" or line[3:5]=="中雨" or line[3:5]=="大雨" or line[3:5]=="暴雨" or line[3:5]=="冰雹" or line[3:5]=="阴天" or line[3:5]=="晴天" or line[3:5]=="小雪" or line[3:5]=="中雪" or line[3:5]=="大雪")):
char2 = line[3:5]
else:
continue
pattern=re.compile(r'\d+')
result=re.findall(pattern,line)
print(result)
print(len(result))
if len(result):
worksheet.write(ii, 0, label=char1)
worksheet.write(ii, 1, label=char2)
j=2
while j < len(result)+2:
worksheet.write(ii, j, label=result[j-2])
j=j+1
else:
worksheet.write(ii, 0, label=char1)
worksheet.write(ii, 1, label=char2)
ii = ii + 1
workbook.save('C:/Users/w1248/Desktop/py写入文件测试.xls')
注意事项:
1.需要利用pycharm的工程环境下运行
2.需要安装基于python的扩展包,其中包括xlwt和openpyxl