Python解析一个文本文件
文件格式如下:
def do_array(astr):
import re
bstr = re.sub(r'{', r'[', astr)
cstr = re.sub(r'}', r']', bstr)
a = eval(cstr)
return a
with open('file.txt') as fd:
for line in fd:
line = line.rstrip()
a = do_array(line)
print a
>>> astr = '''
... {{10, 9}, {6, 4}, {5, 0}, {5, 2}, {7, 6}, {1, 7}, {9, 7}, {2, 8}, {10, 1},
... {5, 9}, {9, 2}, {8, 10}, {3, 2}, {10, 4}, {1, 10}, {10, 9}, {8, 5}, {5, 5},
... {4, 0}, {1, 5}, {0, 6}, {7, 2}, {0, 2}, {10, 7}, {0, 3}, {7, 0}, {3, 5}, {4,
... 9}, {4, 10}, {0, 7}, {8, 10}, {0, 3}, {4, 10}, {0, 3}, {10, 3}, {7, 0}, {2,
... 6}, {3, 9}, {3, 1}, {4, 0}, {1, 1}, {8, 9}, {1, 0}, {0, 3}, {8, 4}, {0, 7},
... {1, 3}, {3, 10}, {5, 7}, {7, 4}}
... '''
>>> def do_array(astr):
... import re
... bstr = re.sub(r'{', r'[', astr)
... cstr = re.sub(r'}', r']', bstr)
... a = eval(cstr)
... return a
...
>>> astr = astr.strip()
>>> a = do_array(astr)
>>> a
[[10, 9], [6, 4], [5, 0], [5, 2], [7, 6], [1, 7], [9, 7], [2, 8], [10, 1], [5, 9], [9, 2], [8, 10], [3, 2], [10,
4], [1, 10], [10, 9], [8, 5], [5, 5], [4, 0], [1, 5], [0, 6], [7, 2], [0, 2], [10, 7], [0, 3], [7, 0], [3, 5], [4
, 9], [4, 10], [0, 7], [8, 10], [0, 3], [4, 10], [0, 3], [10, 3], [7, 0], [2, 6], [3, 9], [3, 1], [4, 0], [1, 1],
[8, 9], [1, 0], [0, 3], [8, 4], [0, 7], [1, 3], [3, 10], [5, 7], [7, 4]]
>>>
def fun():
data=[]
f=open('xxx.txt','r')
for line in f:
start=1 #{1,3},{3,4},...
end=-2 #...,{1,2}, or ...,{3,4}}
if line[0]=='{' and line[1]=='{':
start=2 ##{{1,2},{3,4},...
s=line[start:end].split('},{')
#there is no '{}' now
for item in s:
ss=item.split(',')
data.append([int(ss[0]),int(ss[1])]
return data
#!/usr/bin/env python
import re
avec = []
spat = re.compile(r'^\{\s*\{')
epat = re.compile(r'\}\s*\}$')
dpat = re.compile(r'\{(\d+),\s*(\d+)\},?')
lpat = re.compile(r'\{(\d+),\s*(\d+)\},?$')
def do_vector(astr, lvec):
m = dpat.findall(astr)
if m:
for (x, y) in m:
lvec.append([int(x), int(y)])
return True
def do_file(name):
global avec
avec = []
with open(name, 'r') as fd:
astr = ''; lvec = []
for line in fd:
line = line.strip()
if spat.search(line):
if astr or lvec:
print("Error file format")
lvec = []; astr = line
elif epat.search(line):
astr += line
do_vector(astr, lvec)
if lvec:
avec.append(lvec); lvec = []
astr = ''
else:
astr += line
if lpat.search(astr):
if do_vector(astr, lvec):
astr = ''
if __name__ == '__main__':
do_file('vector.txt')
print(avec)