如何在python中读取ascii格式的表
我有一些以下格式的配置数据。用python解析这些数据的最佳方法是什么?我检查了如何在python中读取ascii格式的表,python,csv,Python,Csv,我有一些以下格式的配置数据。用python解析这些数据的最佳方法是什么?我检查了csv模块,并简要检查了该模块。不知道如何使用它。现有的解析器是用perl编写的 |------------+-----------------+--------| | ColHead1 | Col_______Head2 | CH3 | |------------+-----------------+--------| | abcdefg000 | * | somev1 | |
csv
模块,并简要检查了该模块。不知道如何使用它。现有的解析器是用perl编写的
|------------+-----------------+--------|
| ColHead1 | Col_______Head2 | CH3 |
|------------+-----------------+--------|
| abcdefg000 | * | somev1 |
| abcdefg001 | * | somev2 |
| abcdefg002 | * | |
| abcdefg003 | * | |
| abcdefg004 | * | |
| abcdefg005 | * | |
| abcdefg006 | * | |
| abcdefg007 | * | |
| abcdefg008 | * | |
| abcdefg009 | * | |
| abcdefg010 | * | |
|------------+-----------------+--------|
|------------+-----------------+--------|
|冷水头1 |冷水头2 | CH3|
|------------+-----------------+--------|
|abcdefg000 |*| somev1|
|abcdefg001 |*| somev2|
|abcdefg002 |*||
|abcdefg003 |*||
|abcdefg004 |*||
|abcdefg005 |*||
|abcdefg006 |*||
|abcdefg007 |*||
|abcdefg008 |*||
|abcdefg009 |*||
|abcdefg010 |*||
|------------+-----------------+--------|
您可以尝试类似的方法:
def parse(ascii_table):
header = []
data = []
for line in filter(None, ascii_table.split('\n')):
if '-+-' in line:
continue
if not header:
header = filter(lambda x: x!='|', line.split())
continue
data.append(['']*len(header))
splitted_line = filter(lambda x: x!='|', line.split())
for i in range(len(splitted_line)):
data[-1][i]=splitted_line[i]
return header, data
下面是另一种(类似的)方法
如果它位于文件中:
with open(filepath) as f:
for line in f:
if '-+-' in line or 'Head' in line:
continue
# strip '|' off the ends then split on '|'
c1, c2, c3 = line.strip('|').split('|')
print 'Col1: {}\tCol2: {}\tCol3: {}'.format(c1,c2,c3)
或字符串变量:
for line in ascii_table.split('\n'):
if '-+-' in line or 'Head' in line:
continue
c1, c2, c3 = line.strip('|').split('|')
print 'Col1: {}\tCol2: {}\tCol3: {}'.format(c1,c2,c3)
可能重复的