用Python将文件1中字符串A的第n个外观替换为文件2中字符串B的第n次外观



我有两个文件,文件1包含如下内容:

Boa bla bla
Mother name is Charlotte
Kikikiki
Mother name is Bethany
Oooo
Pppp
Wee
Mother name is Rachel
Bla

文件2看起来像:

Vijdidkd
Father
Father
Ididj
Father

我需要按顺序将文件1中所有母亲的名字复制到文件2中出现"父亲"的行中。

我试过好几件事,但我肯定只是知识不够。

我遇到的问题是,从文件1中,我可以提取信息并按照其中一个答案中的建议保存,但是,我不能将其按正确的顺序和正确的项目传递到文件2上。

我试图从中获取信息的文件有点复杂,我可能试图简化太多。

这是我尝试过的文本,实际上用第一个文件的文本替换了第二个文件中的实例,但如果每个实例的第一个文件中有比第二个更多的元素,它们就会开始混淆。

原始文件如下所示:

<imagenes>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8668.JPG_thumb.jpg" principal="1"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8669.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8682.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8683.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8684.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8685.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8681.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8680.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8679.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8674.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8675.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8676.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8677.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8690.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8687.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8691.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8692.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8670.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8671.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534481/image/IMG_8695.JPG_thumb.jpg" principal="0"/>
</imagenes>
<documentos/>
<enlaces>
<enlace idioma="ru">
http://www.nextproperty.es/details-gb534481.html?lan=gb
</enlace>
<enlace idioma="se">
http://www.nextproperty.es/details-gb534481.html?lan=gb
</enlace>
</enlaces>
</propiedad>
<propiedad id="534483">
<fecha_alta>17/10/2018 19:56</fecha_alta>
<fecha_modificacion>07/11/2018 23:32</fecha_modificacion>
<referencia>LM102</referencia>
<familia id="1">Apartamento</familia>
<operacion id="1">Venta</operacion>
<estado id="SM"/>
<dormitorios>3</dormitorios>
<banos>2</banos>
<aseos/>
<localizacion>
<provincia>Murcia</provincia>
<poblacion id="300081">La Manga</poblacion>
<cp>30384</cp>
<zona/>
<pais>ES</pais>
</localizacion>
<superficies>
<habitable/>
<construida>87</construida>
<parcela>110</parcela>
<cocina/>
<salon/>
<jardin>80</jardin>
<terraza/>
</superficies>
<precio divisa="EUR">119500</precio>
<etiquetas>
<etiqueta>Ocasión</etiqueta>
</etiquetas>
<descripciones>
<descripcion idioma="gb">
<titulo>3 bedroom ground floor apartment with large garden</titulo>
<descripcion>
This lovely 3 bedroom ground floor apartment in Mar de Cristal is in a great condition, ready to move into. It has a large terraced garden with entrance for a car, located in a very quiet area, inside a block with a communal pool and safe for children to play around. It features a light and spacious living room, 3 double bedrooms, 2 full bathrooms, a fully equipped kitchen and has central A/C throughout the apartment[IW]
</descripcion>
</descripcion>
</descripciones>
<imagenes>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8695.JPG_thumb.jpg" principal="1"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8712.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8717.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8711.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8709.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8710.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8696.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8697.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8698.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8699.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8700.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8701.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8703.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8704.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8705.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8706.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8707.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8708.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8713.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8714.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8715.JPG_thumb.jpg" principal="0"/>
<imagen url="https://storage.googleapis.com/static.inmoweb.es/clients/1652/property/534483/image/IMG_8716.JPG_thumb.jpg" principal="0"/>
</imagenes>
<documentos/>

我希望输出的文件如下:

<property last_updated_at="2014-01-26T20:30:03Z">
<reference>T800</reference>
<is_sale>true</is_sale>
<sale_price hide="false">49000</sale_price>
<property_type>Apartamento</property_type>
<bedrooms>2</bedrooms>
<bathrooms>1</bathrooms>
<province>Alicante</province>
<city>Torrevieja</city>
<latitude />
<longitude />
<build_size>55</build_size>
<plot_size />
<terrace_size>5</terrace_size>
<parking_spaces />
<floor_number />
<furnished>true</furnished>
<descriptions>
<description language="en">
<text>
Bonito apartamento en Calle Clemente Gonzalvez de dos dormitorios, ba&#241;o, cocina equipada, comedor estar y terraza de 5 m2. A 5 minutos a pie de la plaza del Ayuntamiento.Rodeado de todos los servicios. Ahora en oferta, antes 54.000&#8364; ahora 49.000 &#8364;. Buen estado. Ideal Par vivir todo el a&#241;o o vacaciones.[IW]
</text>
</description>
</descriptions>
<photos>
<photo id="1" />
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
</photos>
</property>

<property last_updated_at="2014-01-26T20:30:03Z">
<reference>T702</reference>
<is_sale>true</is_sale>
<sale_price hide="false">65000</sale_price>
<property_type>Apartamento</property_type>
<bedrooms />
<bathrooms />
<province>Alicante</province>
<city>Torrevieja</city>
<latitude />
<longitude />
<build_size>60</build_size>
<plot_size />
<terrace_size />
<parking_spaces />
<floor_number />
<furnished>true</furnished>
<descriptions>
<description language="en">
<text>
Bonito apartamento de 2 dormitorios con piscina comunitaria en Nueva Torrevieja. Cerca de todos los servicios y en perfecto estado.[IW]
</text>
</description>
</descriptions>
<photos>
<photo id="1" />
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
<photo></photo>
</photos>
</property>

现在发生的情况是,原始文件中的所有标签都位于第二个文件中的标签的位置,然而,这并没有考虑到第一组标签可能有8个元素,我需要将标签的最后2个元素留空,等待移动到下一组标签,以便再次开始粘贴。

这是我目前正在使用的代码,我正面临这个问题。抱歉问了这么长的问题。

with open('inmoweb.xml', 'r') as origin :
data = origin.readlines()
mothers = ''
for line in data :
if '<imagen url' in line :
mothers += (line)
mothers += 'n'
origin.close()
with open('./RESULTS/RESULT_TOTAL.xml', 'r') as f :
data2 = f.readlines()
f.close()

yt=0
with open('./RESULTS/RESULT_TOTAL.xml', 'w') as f :
for line in data2 :
if '<photo />' in line :
f.write(mothers.splitlines()[yt])
f.write('n')
else :
f.write(line)
if (yt+1) < len(mothers.splitlines()):
yt+=1

你能更准确地描述一下你的问题吗?你想要这样的结果还是其他什么?

Vi
jdidkd
Father   --change to--> mother name1, name2, name3.. 
Father   --change to--> mother name1, name2, name3.. 
Ididj
Father   --change to--> mother name1, name2, name3.. 

这可能是最简单的方法:

with open('file1.txt', 'r') as f:
data = f.readlines()
mothers = ''
for line in data:
if line.find('Mother name is '):
line.replace('Mother name is ', '')
mothers += (line + ', ')
mothers += 'n'
f.close()
with open('file2.txt', 'r') as f:
data = f.readlines()
f.close()
with open('file2.txt', 'w') as f:
for line in data:
if line == 'Fathern':
f.write(mothers)
else:
f.write(line)

虽然我仍在努力理解手机上的方法和目标,但在我测试之前,这里已经有一些想法可能会有所帮助…:

  • 文件2中没有一行包含">
  • 有了用于打开文件的With块,您不需要调用close,它会自动为您完成
  • 您可以直接迭代文件对象,不需要readlines这样做

最新更新