gpt4 book ai didi

python - RE 公式获取 2 个字符串并放入 csv

转载 作者:行者123 更新时间:2023-11-30 22:29:48 26 4
gpt4 key购买 nike

我的文本文件中有以下内容,我需要获取一个带有 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>
代码

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 Services\AirportLayout.TAB

等等

我尝试了以下重新但没有得到结果。

'.([^]*)'

我能做什么?我需要将数据源名称和文件名放在一起。

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

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

最佳答案

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

关于python - RE 公式获取 2 个字符串并放入 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46254141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com