May I know how to create / generate XML using elementtree - python-3.x

...
para1= ET.SubElement(root,'para')
anchortag=ET.SubElement(para1,'anchor')
anchortag.set()
para1.text= " sometexthere"
I tried with the above code snippet,but couldn't get the expected output.I don't want to create a new para tag that would take the text to newline.
result of above code :
<para> sometexthere<anchor> xyxyy</anchor></para>
Expected code
<para><anchor> xyxyy</anchor> sometexthere </para>

You have to make a couple of changes, and it should work:
First, change
anchortag.set()
to
anchortag.text='xyxy'
and then, given that you want sometexthere to be at the tail end of the element,
change
para1.text= " sometexthere"
to
para1.tail= "sometexthere"

Related

Replacing a certain part of string with a pre-specified Value

I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"

How to convert selenium webelelements to list of strings in python

I have gathered obligatory data from the scopus website. my outputs have been saved in a list named "document". when I use type method for each element of this list, the python returns me this class:
"<class'selenium.webdriver.firefox.webelement.FirefoxWebElement'>"
In continius in order to solve this issue, I have used text method such this:
document=driver.find_elements_by_tag_name('td')
for i in document:
print i.text
So, I could see the result in text format. But, when I call each element of the list independently, white space is printed in this code:
x=[]
for i in document:
x.append(i.text)
print (x[2]) will return white space.
What should I do?
As you have used the following line of code :
document=driver.find_elements_by_tag_name('td')
and see the output on Console as :
"<class'selenium.webdriver.firefox.webelement.FirefoxWebElement'>"
This is the expected behavior as Selenium prints the reference of the Nodes matching your search criteria.
As per your Code Attempt to print the text leaving out the white spaces you can use the following code block :
x=[]
document = driver.find_elements_by_tag_name('td')
for i in document :
if (i.get_attribute("innerHTML") != "null") :
x.append(i.get_attribute("innerHTML"))
print(x[2])
My code was correct. But, the selected elements for displaying were space. By select another element, the result was shown.

Optional lines in Ultisnips using parameters

I am trying to have some additional lines inserted in snippets based on a parameter. I am not sure how design such snippet.
snippet 'mysnip' 'snippets with optional lines'
This snippet line1 is inserted by default
<This line1a should be inserted if parameter1 is true>
This snippet line2 is inserted by default
<This line2a should be inserted if parameter1 is true>
endsnippet
It is not very clear to me how/where you want to enter your parameters.
One option is to define two snippets, one called mysnip and the other one mysnip1 - in this case you pass the parameter in the snippet name, and the definition of these two snippets should be straightforward.
Another option is to just define one snippet mysnip, and pass the parameter somewhere within this snippet. A working example could look like this:
snippet mysnip1
${1:Change this snippet line to have the text "True" (without quotes).}
This line is always present. `!p
if t[1]=="True":
snip += "A line displayed when $1 has the text True.
`
endsnippet
You can fake this using regular expression triggers. It only works if you do not want to have tabstops in your optional arguments though:
snippet /mysnip([a-z]*)/ "Optionals" r
this is always here!`!p
if "a" in match.group(1):
snip += "only when a"
if "b" in match.group(1):
snip += "only when b"`
endsnippet
If you type mysnip it will just be the first line, mysnipb the first and thirdm and mysnipab will be all of it.
Can't you put your optional lines in a variable that your snippet engine will expand?
In case it doesn't join automatically empty lines produced from a empty variable, you may have to have your variable contain a newline character and put it or the line before/after.

Importing String with variables from Txt file

I need to import text from txt file with some variables. I use BufferedReader and File Reader. In code I have :
String car = "vw golf";
String color = "nice sunny blue color";
And in my txt file:
I have nice " +car+ " which has "+color+".
My expected output :
I have nice vw golf which has nice sunny blue color.
My actual output is :
I have nice " +car+ " which has "+color+".
If I've understood correctly, what you want to do is replace " + car + " with the value of your car string and likewise for colour. You've tried to do this by writing your text file as if it were a command to be evaluated. However, that won't happen - it will just be outputted as is. I'm going to assume you are using c#. What you need to do is, prior to outputting your string, parse it to replace the markers with the variables. I would recommend you get rid of the double quotes in your text file. You could then do something like this:
string text = this.ReadTextFromFile();
string ammended = text.Replace("+car+", car);
As mentioned, this is assuming you remove the double quotes from your text file so it reads:
I have nice +car+ which has +color+.
Also, you don't need to use the + symbols, but I suppose they are a good way of designating a unique token to be replaced. You could use {car} in the file and then likewise in the Replace startment, for example.
I may not have properly understood what you wanted to do, of course!
Edit: Incase of confustion,
this.ReadTextFile();
was just a short hand way of saying that the text variable contains the contents as read from your text file.

Replace text inside div

I have a lot of html files that I want to add a rel="nofollow" to all the a href tags that are into a specific div.
I think c# code can do it. But how do I relate only partial code..?
Any suggestion? also I didnt realy know
Here is what I think:
Parse the HTML line by line
Look for " block start you would need to find the means look for "/div>".
Store all the content between "" into a string
check if "href=" is found and how many
Now parse this string again to search all "" and match with "href=" counter
#5 will give you an array of "href=" tag based lines
Now you can assume every "href=" tag must have ">" at the end of the "
At the last this is what you can do:
string s1 = " this is link ";
string s2 = s1.Insert(s1.IndexOf(">"), " rel=\"nofollow\"");

Resources