RE公式得到2个字符串并放入csv

时间:2017-09-16 12:53:57

标签: python regex

我在文本文件中有以下内容,我需要使用DataSourceName,FileName

获得一个简单的csv

数据结构网络  <DataSourceDefinitionSet> <TABFileDataSourceDefinition id="id1" readOnly="false"> <DataSourceName>AirportLayout</DataSourceName> <FileName>\\GIS\GIS\Corporate Services\Information Services\AirportLayout.TAB</FileName> </TABFileDataSourceDefinition> <TABFileDataSourceDefinition id="id2" readOnly="false"> <DataSourceName>Asset_Toilets</DataSourceName> <FileName>\\gis\gis\CITY WORKS\Infrastructure Management\Asset_Toilets.TAB</FileName> </TABFileDataSourceDefinition> <TABFileDataSourceDefinition id="id3" readOnly="false"> <DataSourceName>BaseLayer_Text</DataSourceName> <FileName>\\GIS\GIS\Corporate Services\Information Services\BaseLayer_Text.TAB</FileName> </TABFileDataSourceDefinition> CODE

import re
filename='CRC_Public_Features.mws'
input_file = open(filename)
count=0
for line in input_file:
    line = line.rstrip()
    if re.search('<FileName>', line) :
        line=line.replace('<Filename>','')
        count+=1
        print str(count)+','+line

输出

>>> 
*** Remote Interpreter Reinitialized  ***
>>> 

1,      <FileName>\\GIS\GIS\Corporate Services\Information Services\AirportLayout.TAB</FileName>
2,      <FileName>\\gis\gis\CITY WORKS\Infrastructure Management\Asset_Toilets.TAB</FileName> 3,

我想要

  

1,AirportLayout,\ GIS \ GIS \ Corporate Services \ Information   服务\ AirportLayout.TAB

我尝试了以下方法但没有结果。

&#39;。([^] *)&#39;

我该怎么办?我需要数据源名称和文件名的两行。

===== 最终使用的代码基于已接受的答案

import re
filename='CRC_Public_Features.mws'
data = open(filename).read()
count=0
#for line in infile:
#data=line
values = [re.findall(first+"(.*?)"+second, data) for first, second in [("<{}>".format(b), "</{}>".format(b)) for b in ["DataSourceName","FileName"]]]
ids = [re.search("\d+", i).group(0) for i in re.findall('id="(.*?)"', data)]
final_values = [ids[0]] + [i[0] for i in values]
DataSourceName=values[0]
FileName=values[1]
total=len(FileName)
with open("Output.csv", "w") as text_file:
        text_file.write("ID,DataSourceName,FileName,MWS\n")
for item in FileName:
    print str(count+1)+","+str(DataSourceName[count])+","+str(FileName[count])
    with open("Output.csv", "a") as text_file:
        text_file.write(str(count+1)+","+str(DataSourceName[count])+","+str(FileName[count])+","+str(filename)+"\n")
    count+=1

2 个答案:

答案 0 :(得分:2)

使用xml.etree.ElementTreecsv模块:

import xml.etree.ElementTree as ET, csv

tree = ET.parse('CRC_Public_Features.mws')
root = tree.getroot()

with open('result.csv', 'w', newline='') as f:
    writer = csv.writer(f, delimiter=',')
    for i,ds in enumerate(root.findall('TABFileDataSourceDefinition'), 1):
        writer.writerow([i, ds.find('DataSourceName').text, ds.find('FileName').text])

最终result.csv内容:

1,AirportLayout,\\GIS\GIS\Corporate Services\Information Services\AirportLayout.TAB
2,Asset_Toilets,\\gis\gis\CITY WORKS\Infrastructure Management\Asset_Toilets.TAB
3,BaseLayer_Text,\\GIS\GIS\Corporate Services\Information Services\BaseLayer_Text.TAB

答案 1 :(得分:1)

你可以试试这个:

import re
filename='CRC_Public_Features.mws'
data = open(filename).read()
values = [re.findall(first+"(.*?)"+second, data) for first, second in [("<{}>".format(b), "</{}>".format(b)) for b in ["DataSourceName","FileName"]]]
ids = [re.search("\d+", i).group(0) for i in re.findall('id="(.*?)"', data)]
final_values = [ids[0]] + [i[0] for i in values]

输出:

['1', 'AirportLayout', '\\GIS\\GIS\\Corporate Services\\Information Services\\AirportLayout.TAB']