How to remove a directory path from a text file - python-2.7

I have a text file with these lines.
1.inputlist
D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod21_I0D0CRC.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Dolby\m1_m28_switch.cfg
2.inputlist
D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod_2_252_ddp.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Digital\m1_m7_switch_every_3frames.cfg
Here i need to remove the path names like
"D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\"
and "D:\Dolby_Harmanious_kit\DRY_run_kits\Digital\ "
.Note that all lines have a different path names.I have a example code to remove a path name.
Code:
import re
b = open ('Filter_Lines.txt','w')
with open('Lines.txt') as f:
for trim in f:
repl = (re.sub('D:.*\\\\','',trim).rstrip('\n'))
b.write(repl + '\n')
b.close()
But here this removes a whole text from "
D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod21_I0D0CRC.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Dolby\"
.I need to remove only path names not including "acmod21_I0D0CRC.ec3" in that line.
Can you please guide me for this.

I did upto what i understand your question,
here you specified path's are not similar i.e what i understood is,
your path might be
a) D://a/b/c/file_name.cfg -#tD://a/b/c/d/e/file_name.cfg
is it correct what i understood?
here 2 path present in single line, but common thing is its contains -#t,
so simply use split method to split that.
here what i did based i understand from your post,
import re
li = []
b = open ('file_sample.txt','w')
with open ('file_sam.txt') as f:
for i in open ('file_sam.txt','r'):
a = [re.sub('.*\\\\','',i).rstrip('\n') for i in i.split('D:')]
b.write(''.join(a) + '\n')
b.close()
here my inputs are,
'D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod21_I0D0CRC.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Dolby\m1_m28_switch.cfg'
'D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod_2_252_ddp.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Digital\m1_m7_switch_every_3frames.cfg'
it gives me,
'acmod21_I0D0CRC.ec3 -#tm1_m28_switch.cfg'
'acmod_2_252_ddp.ec3 -#tm1_m7_switch_every_3frames.cfg'
is this you want?

Related

PowerBI: Check if the folder/directory we are loading to exists or not using Power Query

I am a newbie in PowerBi and currently working on a POC where I need to load data from a folder or directory. Before this load, I need to check if
1) the respective folder exists
2) the file under the folder is with.csv extension.
Ex. Let suppose we have a file '/MyDoc2004/myAction.csv'.
Here first we need to check if MyDoc2004 exists and then if myAction file is with.csv extension.
Is there any way we can do this using Power Query?
1. Check if the folder exists
You can apply Folder.Contents function with the absolute path of the folder, and handle the error returned when the folder does not exist with try ... otherwise ... syntax.
let
absoluteFolderPath = "C:/folder/that/may/not/exist",
folderContentsOrError = Folder.Contents(absoluteFolderPath),
alternativeResult = """" & absoluteFolderPath & """ is not a valid folder path",
result = try folderContentsOrError otherwise alternativeResult
in
result
2. Check if the file is with .csv extension
I'm not sure what output you are expecting.
Here is a way to get the content of the file by full path including ".csv", or return an alternative result if not found.
let
absoluteFilePath = "C:/the/path/myAction.csv",
fileContentsOrError = File.Contents(absoluteFilePath),
alternativeResult = """" & absoluteFilePath & """ is not a valid file path",
result = try fileContentsOrError otherwise alternativeResult
in
result
If this is not what you are looking for, please update the question with the expected output.
Hope it helps.

Python Dictionary re

As you can see in code using regular expression script searches for words in txt and puts them to dictionary like this:
set(["['card', 'port']"])
set(["['onu_id', 'remote_id']"])
set(["['card', 'port', 'onu_id']"])
set(["['card', 'port', 'onu_id']"])
set(["['remote_id']"])
set(["['remote_id']"])
set(["['card', 'port', 'onu_id']"])
Problem is that i need to input values to them by hand and remove everything except keys(card,port,onu_id,remote_id)(remove: set(["[' to see everything clearly:
dict{card:1, port:5, onu_id:3, remote_id:16568764}
To look like this and be easy to read.
Here is my code:
import re, string
with open("conf.txt","r") as f:
text = f.readlines()
for line in text:
match = re.findall(r'_\$(\w+)',line)
if match:
dict = {str(match)}
print dict
part of input file:
interface gpon-olt_1/_$card/_$port
onu _$onu_id type ZTE-F660 pw _$remote_id vport-mode gemport
no lock
no shutdown
exit
interface gpon-onu_1/_$card/_$port:\_$onu_id
exit
interface gpon-onu_1/_$card/_$port:\_$onu_id
name ONU-_$remote_id 102211+1
description
vport-mode gemport def-map-type 1:1

how to replace a text on XML with lxml?

I'm trying to troncate several element.text on a xml file. I succeed to get two list, the first one regroup the formers too long element.text as str (long_name) and the second regroup the same after a troncation (short_name).
Now i want to replace the element.text on my xml, i tried some script but i surrended to work with the function readlines(), i want to find a similar solution with lxml as this code :
txt = open('IF_Generic.arxml','r')
Lines = txt.readlines()
txt.close()
txt = open('IF_Genericnew.arxml','w')
for e in range(len(long_name)) :
for i in range(len(Lines)) :
if (long_name[e] in Lines[i]) == True :
Lines[i] = Lines[i].replace(long_name[e],short_name[e])
for i in Lines :
txt.write(i)
txt.close()
I tried this, but it doesn't work :
f = open('IF_Generic.arxml')
arxml = f.read()
f.close()
tree = etree.parse(StringIO(arxml))
for e,b in enumerate(long_name) :
context = etree.iterparse(StringIO(arxml))
for a,i in context:
if not i.text:
pass
else:
if (b in i.text) == True :
i.text = short_name[e]
obj_arxml = etree.tostring(tree,pretty_print=True)
f = open('IF_Genericnew.arxml','w')
f.write(obj_arxml)
f.close()
Let's say the first element of the list long_name is RoutineServices_EngMGslLim_NVMID03
<BALISE_A>
<BALISE_B>
<SHORT-NAME>RoutineServices_EngMGslLim_NVMID03</SHORT-NAME>
</BALISE_B>
</BALISE_A>
<BALISE_C>
<POSSIBLE-ERROR-REF DEST="APPLICATION-ERROR">/Interfaces/RoutineServices_EngMGslLim_NVMID03/E_NOT_OK</POSSIBLE-ERROR-REF>
<SHORT-NAME>Blah_Bleh_Bluh</SHORT-NAME>
</BALISE_C>
The first element of the list short_name is RoutineServices_EngMGslLim_NV
<BALISE_A>
<BALISE_B>
<SHORT-NAME>RoutineServices_EngMGslLim_NV</SHORT-NAME>
</BALISE_B>
</BALISE_A>
<BALISE_C>
<POSSIBLE-ERROR-REF DEST="APPLICATION-ERROR">/Interfaces/RoutineServices_EngMGslLim_NV/E_NOT_OK</POSSIBLE-ERROR-REF>
<SHORT-NAME>Blah_Bleh_Bluh</SHORT-NAME>
</BALISE_C>
I want this
P.S: I use python 2.7.9
Thanks in advance everyone !
Don't open XML files like text files. I have explained in this answer why this is a bad idea.
Simply let etree read and write the file. It's also less code to write.
from lxml import etree
# read the file and load it into a DOM tree
tree = etree.parse('IF_Generic.arxml')
for elem in tree.iterfind("//*"):
# find elements that contain only text
if len(elem) == 0 and elem.text and elem.text.strip() > '':
# do your replacements ...
elem.text = "new text"
# serialize the DOM tree and write it to file
tree.write('IF_Genericnew.arxml', pretty_print=True)
Instead of going over all elements, which is what "//*" does, you can use more specific XPath to narrow down the elements you want to work on.
For example, something like "//SHORT-NAME | //POSSIBLE-ERROR-REF" would help to reduce the overall work load.

QFileSystem entryInfo not returning full directories name

So i have some duplicate songs in my Itunes library (844 to be precise) so i thought I would write a small program to find and destroy the duplicates. The problem I have encountered is some of the directories are not being read correctly.
For example one of album's folder name is "Greatest Hits, Vol. 2" But is read "Greatest Hits, Vol"
Obviously the "." is causing a problem. Does anyone know of a workaround that will allow me to read the full file path name (with periods)?
small snipit of Code
QString name;
int id;
for (int a = 0; a < dir.entryList(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot).length(); ++a)
{
name = path() + "/" + baseName() + "/" + dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot).at(a);
subFiles.append(new CKClone(id, path() + "/" + baseName() + "/" + dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot).at(a), this));
// Updating the ID number to account for descendants.
id = subFiles.last()->countDescendants(id);
}
The above code is just a small snipit and obviously doesn't do a whole bunch on its own. It simply reads in all of the files and directories recursively.
Any help will be appreciated, Thanks
Jec
I actually tested this in PyQt and found that the problem does not occur for me. This is tested using Qt 4.8, so you may want to check if its a problem specific to your version.
Dir structure like this:
testing/
a path with spaces/
patH_with_no_spaces/
path with version, vol. 2/
Test snippet
from PyQt4 import QtCore
d = QtCore.QDir("testing")
list(d.entryList(d.AllEntries|d.NoDotAndDotDot))
# output
[
PyQt4.QtCore.QString(u'a path with spaces'),
PyQt4.QtCore.QString(u'path with version, vol. 2'),
PyQt4.QtCore.QString(u'patH_with_no_spaces')
]
You might want to first try simplifying your code a bit more to see if its a bug in your code. You seem to be regenerating your list every time you loop, build a string, and append. Try saving the QStringList result of that entryList call once before your loop, and using that same object for the rest of the code.

Does Qt Linguist offer the ability to add new entries to the editable .ts file?

I didn't find a way to do this - only to edit the translations to the existing fields.
If there is no way to achieve this - how should this be done (somehow automatically, because right now I was manually adding
<message>
<source>x</source>
<translation>xx</translation>
</message>
blocks to my .ts file and I assume that's not the correct way.
No, that's not the correct way :) Use tr() in the code to mark strings for translation.
For example
label->setText( tr("Error") );
The you run lupdate for your project to extract them to a .ts. See here for more details.
Or do you need to translate strings that are not in the source code?
I just wrote a python script to insert new entries
into the .ts file for a homegrown parser using ElementTree. It doesnt make the code pretty
when it adds it, but I believe it works just fine (so far):
from xml.etree import ElementTree as ET
tree = ET.parse(infile)
doc = tree.getroot()
for e in tree.getiterator()
if e.tag == "context":
for child in e.getchildren():
if child.tag == "name" and child.text == target:
elem = ET.SubElement(e, "message")
src = ET.SubElement(elem, "source")
src.text = newtext
trans = ET.SubElement(elem, "translation")
trans.text = "THE_TRANSLATION"
tree.write(outfile)
Where infile is the .ts file, outfile may be the same as infile or different.
target is the context you are looking for to add a new message into,
and newtext is of course the new source text.