xlwt use Hyperlink with variable - python

I would like to use xlwt to generate hyperlink in particular cell. I tried to put following in formula, it's fine:
Hyperlink("http://www.google.com";"Link")
But when I define X='"http://www.google.com"' (Note the single quote outside of double quote)
and then:
Hyperlink(X;"Link")
It won't work.
Basically, I want to put a variable X, which could be different when the program runs, into Hyperlink(). Any idea to fix this problem would be appreciated!

Use this construct
click='"http://www.google.com"'
wsheet.write(j,8,xlwt.Formula('HYPERLINK(%s;"Link")' % click))
or, easier to read and maintain:
click='http://www.google.com'
wsheet.write(j,8,xlwt.Formula('HYPERLINK("%s";"Link")' % click))
For details of the % operator for string formatting, see
http://docs.python.org/2/library/stdtypes.html#string-formatting

Related

Issue with strings and dictionaries in python

I'm using the click package to get input for one or more variables which get loaded in as a combined dictionary. Each entry is then joined and the combined string is added to the end of a base URL and sent through the requests package to receive some xml data.
Earlier I had an issue with one of the variables that let you search through a range, such as
[value1, value2]
Python added double quotes around it so the search function didn't operate correctly, so I used
.replace('"', '')
on the joined string before combined with the base url and that seemed to fix that problem. The issue now is that individual input that contains more than one word now doesn't produce the same output as the actual search engine online. I have to use quotes when I input the information to keep it as a single argument, but then the quotes get removed by the function above and I believe that is what is causing the issue.
I think if I have a way to access individual entries of this dictionary and remove the double quotes from only certain entries then that should get the job done. But if I am overlooking something please let me know.
Help is appreciated.
Code added below:
import click
import requests
#click.command()
#click.option(--variable1)
#click.option(--variable2)
query_list=[variable1, variable2]
query=''.join(query_list)
base_url = "abc.com...."
response=requests.get(base_url,query)

Python for maya: Why can't I use a variable in concatination with a wildcard?

I'm trying to use the "ls" python command in maya, to list certain objects with a matching string in the name in concatination with a wildcard.
Simple sample code like this:
from maya.cmds import *
list = ls('mesh*')
This code works and will return a list of objects with the matching string in the name, however, I would like to use a variable instead of hard coding in the string. More like this:
from maya.cmds import *
name = 'mesh'
list = ls('name*')
OR like this:
from maya.cmds import *
name = 'mesh'
list = ls('name' + '*')
However, in both examples, it returns an empty list unlike the first. I'm not sure why this is the case because in those examples, the string concatination should come out to 'mesh*' like the first example. I couldn't find an answer on this website, so I chose to ask a question.
Thank you.
JD
PS. If there is a better way to query for objects in maya, let me know what it's called and I'll do some research into what that is. At the moment, this is the only way I know of how to search for objects in maya.
As soon as you add quotes around your variable name like this 'name', you are actually just creating a new string instead of referring to the variable.
There are many different ways to concatenate a string in Python to achieve what you want:
Using %:
'name%s' % '*'
Using the string's format method:
'{}*'.format(name)
Simply using +:
name + '*'
All of these will yield the same output, 'mesh*', and will work with cmds.ls
Personally I stick with format, and this page demonstrates a lot of reasons why.

how to convert string contains list to regular list in python?

I have an the following function in Apache Airflow:
from airflow.utils.email import send_email
send_email(to=['a#mail.com','b#mail.com'],
subject=....)
This works great.
Now I don't want to hard code the email list so I save it as a configurable field that the user can change from his UI.
So I change my code to:
NOTIFY_LIST = Variable.get("a_emails")
send_email([NOTIFY_LIST],
subject=....)
But this doesn't work.
When I do:
logging.info(NOTIFY_LIST)
logging.info([NOTIFY_LIST])
logging.info(NOTIFY_LIST.split(','))
I see:
'a#mail.com', 'b#mail.com'
[u"'a#mail.com', 'b#mail.com'"]
[u"'a#mail.com'", u" 'b#mail.com'"]
So my problem is that:
['a#mail.com','b#mail.com']
and
[NOTIFY_LIST]
isn't the same.
How can I fix this? I tried any conversion I could think of.
A suggestion to try the following;
logging.info(NOTIFY_LIST.replace("'", "").split(','))
The problem here is that the elements in the list contain quote marks.
The other answer will fail if there's an ' in the midle of the strings, to fix that I'll use str.strip:
logging.info([s.strip("' ") for s in NOTIFY_LIST.split(',')])

How to use variables value while sourcing a file in Python script

I am a beginner in Python. I want to source the file which has a variable value. I am sourcing the variable value using command line argument option. I want to use this variable as a path parameter to source my file. I am saying it to the variable. Below is the code I am trying.
folder_name = 'abc'
execfile("/a/b/c/folder_name")
and I am expecting it will take the value of folder_name as abc and execute the file as
execfile("/a/b/c/abc")
This can be done either with string concatenation, or with format - the latter being safer/better:
execfile("/a/b/c/" + folder_name)
execfile("/a/b/c/{}".format(folder_name)
For more info on format, see PyFormat
However you might want to consider if execfile is the right approach here! Other data formats can be a better option - e.g. pickle, json or yaml.
You could use .format() whenever you need to insert variable name into string:
execfile("/a/b/c/{0}".format(folder_name))
Note that {0} here corresponds to first argument that you pass to format function.
Similarly, {1}, {2},... corresponds to second, third,.. arguments of format function respectively.
You could also use % operator, but note that it is deprecated as of Python 3.1. 
As you say you are a beginner in Python then you should change your approach and should not use execfile
Instead consider using pickle or json.load to store data
You need something like this:
folder_name = 'abc'
execfile("/a/b/c/{folder_name}".format(folder_name=folder_name))

xlsxwriter refer to sheets inside worksheet.write_formula()

I am trying to find a solution how to substitute the following:
worksheet = writer.sheets['Overview']
worksheet.write_formula('C4', '=MIN('Sheet_147_mB'!C2:C325)')
with something like:
for s in sheet_names:
worksheet.write_formula(row, col, '=MIN(s +'!C2:C325')')
row+=1
to iterate through all the existing sheets in the current xlsx book and write the function to the current sheet having an overview.
After spending some hours I was not able to find any solution therefore it would be hihgly appriciated if someone could point me to any direction. Thank you!
You don't give the error message, but it looks like the problem is with your quoting - you can't nest single quotes like this: '=MIN(s +'!C2:C325')'), and your quotes aren't in the right places. After fixing those problems, your code looks like this:
for s in sheet_names:
worksheet.write_formula(row, col, "=MIN('" + s +"'!C2:C325)")
row+=1
The single quotes are now nested in double quotes (they could also have been escaped, but that's ugly), and the sheet name is enclosed in single quotes, which protects special characters (e.g. spaces).

Categories