Using a python program to rename all XML files within a linux directory -


Currently, my code uses the name of the XML file as a parameter to take that file, some of it Parsing content and using it to change the name of the file, what I mean is that my program is actually run once and that program will search for every XML file (even if it's inside the zip Too) inside the directory and the same pai Let's name it using the ramometer which is what I'm having problems with.

  # encoding: utf-8 import os, sys import argv with script again, nombre_de_archivo = argv regexFecha = 'r \ "\ d {4} - \ d {2} - \ D {2} 'regexLocalidad = r'localidad = \ "[\ w, S, -_] * \"' regexNombre = r'nombre = \ "[\ w \ s., -_] * \" 'RegexTotal = r'total = \ "\ d +.? \ D + \" 'fechas = []; Local Dindex = []; Nombres = []; totales = [] archivo = open (nombre_de_archivo) for line in archivo.readlines): fechas.append (re.findall (regexFecha, linea)) localidades.append (re.findall (regexLocalidad, linea)) nombres.append (again . Findal (reggex numeral, line)) totales.append (re.findall (regexTotal, linea)) fecha = str (fechas [1] [0]) localidad = str (localidades [1] [0]). Strip ('localidad = \' ') Nombre = str (nombres [1] [0]). Strip (' nombre = \ ') total = str (total [1] [0]). Strip (' total = '') Nombre_nuevo_archivo = fecha + "_" + localidad + "_" + nombre + "_" + total + ". xml "os.rename (nombre_de_archivo, nombre_nuevo_archivo)   

Edit: This will be an example.

The directory contains only 3 files as well as the program.

silly.xml amusing.zip feisty.txt

Therefore, you run the program and ignore this feisty because it is a .txt file and it reads silly.xml, then " Fitas, Lokidad, Numbers, Total "connects or adds or whatever is used and it is used as a new file for silly.xml, The program checks whether zip is an XML file, I do this so it does the same thing.

So we finally

20141211_sonora_walmart_2033.xml 20141008_sonora_starbucks_102.xml feisty txt amusing.zip

Your question is not clear, and the code you posted is very extensive.

I have regular expressions with the power of my vision, but there are many things that you can do to make the code easier. Rutian, and have an easier time debugging.

To find out your target files, use it:

  files = glob.glob ('dir / *. Xml')   

To parse them, eat regular expressions and use the API.

  Import xml.etree.ElementTree ET tree = ET. Pars ('.xml as the target') root = tree.getroot ()   

There are also modules for navigating XML files with XML authoring and XPATH. Using the file name regex is okay by removing the field, but check out.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -