Python系列(4)-- Python 正则表达式匹配字符串替换、格式修改

有如下数据,需要把 . 去掉
这里写图片描述

# -*- coding: utf-8 -*-
"""
Created on Mon Sep 25 20:47:33 2017

@author: Don
"""
import re


f = open("84.txt",'rb')
r = open("84_result.txt","w+")
corpus = bytes.decode(f.read()).split("\n")
s = r'\d{8}.\d{2}'                 #正则表达式匹配
for i in range(len(corpus)):

    m = re.match(s, corpus[i])
    if m is None:                  #要先判断是否为空
        continue
    str = m.group(0)               #得到匹配的字符串
    repStr= str.replace(".","")
    corpus[i] = corpus[i].replace(str, repStr)
    r.write(corpus[i] + '\n')
r.close()
f.close()

有如下数据,需要去掉 . 并把不足十位的最后补零
这里写图片描述

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 26 10:46:12 2017

@author: Don
"""

import re
import copy

f = open("index.txt",'rb')
r = open("index_result.txt","w+")
corpus = bytes.decode(f.read()).split("\n")
corpuscopy = copy.copy(corpus)

for i in range(len(corpus)):
    if corpus[i] is None:             #判断空行
        continue 
    tmp = corpus[i].split()
    if len(tmp) == 0:
        continue
    indextmp = len(tmp[-1])
    if '.' in tmp[-1]:                #定位.  并删除
        index = tmp[-1].find('.')
        tmp[-1] = tmp[-1][:index] + tmp[-1][index+1:]
    if len(tmp[-1]) < 10:             #不足十位的报关码要补全
        tmpstr = ''
        for k in range(10 - len(tmp[-1])):
            tmpstr = tmpstr + '0'
        tmp[-1] = tmp[-1] + tmpstr
    corpuscopy[i] = corpuscopy[i][:len(corpuscopy[i]) - indextmp] + tmp[-1]

    r.write(corpuscopy[i] + '\n')
r.close()
f.close()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值