|
本帖最后由 zst20020306 于 2024-7-24 04:00 编辑
这个程序可以用来方便地将NMC的台风快讯以尽可能还原的格式搬运到论坛内。使用方式如下:
- 在程序目录下面建立一个名为 input.txt 的文本文档。
- 全选NMC官网上的台风快讯并复制
- 将复制的内容粘贴至 input.txt 文件内,替换原有的内容,然后保存
- 运行程序
- 打开程序目录下面的 output.txt 文件,复制里面的全部内容
- 打开论坛,将内容粘贴至文本框内即可
效果图如下:
程序源代码如下:
- import re
- import os
- def extract_numbers(s):
- return [int(num) for num in re.findall(r'\d+', s)]
- def find_positions(string, target):
- positions = []
- for i in range(len(string)):
- if string[i] == target:
- positions.append(i)
- return positions
- file_path = os.path.dirname(__file__)
- path = file_path+"\input.txt"
- with open(path,"r",encoding='utf-8') as file:
- data = file.read()
- nums = extract_numbers(data)
- nums = nums[0:6]
- output1 = "[align=center][b][color=Red][size=6]台风快讯[/size][/color][/b]\n\n[size=3][b]%d年总%d期[/b]\n\n[b]中国气象局中央气象台 %02d月%02d日%02d时%02d分[/b][/align][/size]\n" %(nums[0],nums[1],nums[2],nums[3],nums[4],nums[5])
- xiegangNpos = find_positions(data, '\n')
- input2 = data[xiegangNpos[2]:]
- print(input2)
- leng = len(input2)
- print(leng)
- output2 = input2
- inserted_num = 0
- redflag = 0
- for i in range(leng):
- if input2[i] == '\n':
- i_start = i
- if redflag == 1:
- redflag = 0
- output2 = output2[:i_start+inserted_num] + "[/color][/b]" + output2[i_start+inserted_num:]
- inserted_num = inserted_num + 12
- if input2[i] == ':':
- i_end = i
- output2 = output2[:i_start+inserted_num+1] + "[b]" + output2[i_start+inserted_num+1:]
- inserted_num = inserted_num + 3
- if input2[i-3:i-1] == '心位' or input2[i-3:i-1] == '度等' or input2[i-3:i-1] == '大风':
- output2 = output2[:i_end+inserted_num+1] + "[color=Red]" + output2[i_end+inserted_num+1:]
- inserted_num = inserted_num + 11
- redflag = 1
- else:
- output2 = output2[:i_end+inserted_num+1] + "[/b]" + output2[i_end+inserted_num+1:]
- inserted_num = inserted_num + 4
- if input2[i] == '十' and input2[i+1] == '级':
- output2 = output2[:i_start+inserted_num+1] + " " + output2[i_start+inserted_num+1:]
- inserted_num = inserted_num + 24
- if input2[i] == '十' and input2[i+1] == '二' and input2[i+2] == '级':
- output2 = output2[:i_start+inserted_num+1] + " " + output2[i_start+inserted_num+1:]
- inserted_num = inserted_num + 24
- output=output1+output2[1:]
- print(output)
- file = open(file_path+"\output.txt", "w")
- file.write(output)
- file.close()
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
评分
-
查看全部评分
|