Python正则化匹配读取txt数据转为list列表

1. txt文本数据

今有txt存放的文本数据格式为
Python正则化匹配读取txt数据转为list列表_第1张图片

要求将其数据提取出来,形成坐标点形式
在这里插入图片描述

2. 实现代码

#!/usr/bin/python
# -*-coding:utf-8 -*-
__author__ = 'Alex_XT'
import re

def readTxt(filePath):
    result = []
    with open(filePath, 'r') as f:
        for line in f:
            arrPair = re.findall(r'\d+,\s?\d+', line)
            #print arrPair
            num = list(map(int, arrPair[0].split(',')))
            #print num
            result.append(num)
    return result


if __name__ == '__main__':
    filePath = r"C:\Users\asus\Desktop\0.txt"
    arrList = readTxt(filePath)
    print arrList


ko!

3. 参考文献

【1】python读取TXT每行,并存到LIST中 - 宁不凡的博客 - CSDN博客
https://blog.csdn.net/qq_34489091/article/details/80652653
【2】python正则表达式匹配 模式匹配 - 疯狂的狗会直立行走 - 博客园
https://www.cnblogs.com/lzw121/p/6306698.html
【3】Python正则表达式 - Alex_XT的博客 - CSDN博客
https://blog.csdn.net/u011463646/article/details/78576047

你可能感兴趣的:(Python)