用Python替换文件1中字符串A的第n个出现到文件2中字符串B的第n个出现

时间:2018-11-16 08:43:01

标签: python python-3.x

我有2个文件,文件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中出现“父亲”的行中。

我已经尝试了几件事,但是我只是不确定我是否知道。

我遇到的问题是,我可以从file1中提取信息并按照答案之一的建议进行保存,但是,我无法再将其以正确的顺序和正确的项目传递给file2

我试图从中获取信息的文件有些复杂,也许我试图简化太多。

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

原始文件如下:

    <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

3 个答案:

答案 0 :(得分:1)

这可能是最简单的方法:

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 == 'Father\n':
            f.write(mothers)
        else:
            f.write(line)

答案 1 :(得分:0)

能否请您对问题进行更精确的描述? 您想要这样的结果还是其他?

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

答案 2 :(得分:0)

While I'm still trying to understand approach and goal on my mobile, here already some thoughts that might help until I can test sth... :

  • There's not a single line in file2 which has '' in it.
  • With a with-block for opening files you don't need to call close, it's done for you automatically
  • You can directly iterate over the file object, no need for readlines to do so