使用python 打开文件并做匹配处理的实例

yipeiwu_com6年前Python基础

如下所示:

import os
import re
import string
 
 
file = open("data2.txt")
p1 = re.compile(r"^(\d{16})\s+(\d{3})")
re.compile(p1)
for line in file:
    print(line)
    match1 = re.search(p1,line)
    #print(match1.group(0))
    sCard = match1.group(1)
    sValue=match1.group(2)
    print(sCard)
    print(sValue)
 
    b = re.findall(r"\d{2}",sCard)
    # remove r or one \ if want signle \
    c =r'\\x'.join(b)
    c= r'\\x'+c
    print(c)

以上这篇使用python 打开文件并做匹配处理的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换) Python3 JSON 数据解析...

如何基于python测量代码运行时间

这篇文章主要介绍了如何基于python测量代码运行时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Python 社区有句俗语: “...

使用TensorFlow实现简单线性回归模型

使用TensorFlow实现简单线性回归模型

本文使用TensorFlow实现最简单的线性回归模型,供大家参考,具体内容如下 线性拟合y=2.7x+0.6,代码如下: import tensorflow as tf import...

python和pyqt实现360的CLable控件

 复制代码 代码如下: #!/usr/bin/python  #-*-coding:utf-8-*- from PyQt4.QtGui import *fr...

使用 Python 处理 JSON 格式的数据

如果你不希望从头开始创造一种数据格式来存放数据,JSON 是一个很好的选择。如果你对 Python 有所了解,就更加事半功倍了。下面就来介绍一下如何使用 Python 处理 JSON 数...