I new to python and stuck in for loop function can someone help on on how can I get value of "i" in next function.
formula
from pixelate import pixelate
for i in range(197):
pixelate('/Users/amanrai/Desktop/lady/%i.png', '/Users/amanrai/Desktop/lady/rotate/%i.png', 20)
I want the "i" value in after the directory name
would appreciate if some expert can help me.
you can use
for i in range(197):
img = f'/Users/amanrai/Desktop/lady/{i}.png'
img_rotate = f'/Users/amanrai/Desktop/lady/{i}.png'
pixelate(img, img_rotate, 20)
You can use format:
for i in range(197):
pixelate('/Users/amanrai/Desktop/lady/{}.png'.format(i), '/Users/amanrai/Desktop/lady/rotate/{}.png'.format(i), 20)
See this wonderful cheat sheet for more information.
Use an f-string:
pixelate(f'/Users/amanrai/Desktop/lady/{i}.png', f'/Users/amanrai/Desktop/lady/rotate/{i}.png', 20)
String interpolation or f-string for short allow you to put variables inside a string. This feature was added in Python 3.6. If you are a previous version you must use str.format instead.
Hope this helps:
for i in range(197):
pixelate(f"/Users/amanrai/Desktop/lady/{i}.png', '/Users/amanrai/Desktop/lady/rotate/{i}.png', 20")
Related
I am trying to go through a loop of strings (alias names) in order to apply the unidecode, and to avoid the error of not having aliases words in some cases, I am using an if/else to check if there is a string or not, so I can apply the unidecode.
I tried by checking the length of the word by doing this code, but it keeps saying "list' object has no attribute 'element". I am not sure what should I do.
alias=record.findall("./person/names/aliases/alias")
alias_name=[element.text for element in alias]
if len(alias_name.element.text)!=0:
alias_names_str = ','.join(alias_name)
alias_names_str=unidecode.unidecode(alias_names_str)
else:
alias_name.element.text="NONE"
Not exactly sure what you are trying to accomplish, but it looks like an XML or HTML decoder.
# alias is an iterable of elements
alias=record.findall("./person/names/aliases/alias")
# alias_names is a list of strings, you have already dereferenced the text
alias_names=[element.text for element in alias]
if len(alias_names)>0:
alias_names_str = ','.join(alias_names)
alias_names_str = unidecode.unidecode(alias_names_str)
else:
alias_names_str = "NONE"
thank you! yes, I want to apply the unidecode to remove the diacritics of those strings. Your code helped, then I needed to change some details and it worked!
Thank you so much! :)
so I have a list that holds a tuple
stat_list = [('name', 11212)]
print('{0:>16} {0:17}'.format(stat_list[0][0], stat_list[0][1]))
My issue is that it's printing only "name" for both formats instead of "name" and "11212"
Why? and how could I make it print both? Thank you!
The first number in the bracket group indicates which index in the format parameters to reference. Either remove the numbers or change them to point at the correct parameter. Example below, with alternatives.
# Reference numbers before colon
print("{0:>16} {1:17}".format(stat_list[0][0], stat_list[0][1]))
# No reference goes in order of entry
print("{:>16} {:17}".format(stat_list[0][0], stat_list[0][1]))
# Unpack the tuple instead
print("{:>16} {:17}".format(*stat_list[0]))
# f-strings are a bit more clear (Python 3.6+)
print(f"{stat_list[0][0]:>16} {stat_list[0][1]:17}")
This is your updated code. It will work perfectly.
stat_list = [('name', 11212)]
print('{0:>16} {1:17}'.format(stat_list[0][0], stat_list[0][1]))
Please go through https://www.geeksforgeeks.org/python-format-function/
I am trying to test a Zapier field with python code block to see if it ends with .mov. If so, I want to remove the .mov. If not, just return the field as is. The code I have works, but I have added a .lower() to make it work and don't understand why it behaves that way.
I have tried multiple variations but the only one that seems to work is adding string formatting .lower() in the variable.
formatted_str = input_data['text']
if formatted_str.endswith('.mov'):
formatted_str = formatted_str[:-4]
return {'formatted_str':formatted_str.lower()}
I would like to return the results without having to change it to all lower case. I know it is possible, but I am not having any luck. Thank you so much!
With the help of Zapier folks, I figured out my issue. Thank you!
formatted_str = input_data['text']
if formatted_str.endswith('.mov'):
formatted_str = formatted_str[:-4]
return {'text':formatted_str}
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
I am using a piece of code and am trying to debug it.
But something weird is happening.
In one part, I have this line:
vals = find(A)
But it gives me this error :
global name 'find' is not defined
I thought find was like an inbuilt function in python??
Clearly I am mistaken.
But just want to check.. in case I am forgetting to include anything.
find is a string method:
'python'.find('y') # gives 1
The real answer is:
look in this page:
http://docs.python.org/genindex.html
Why thinking to search with Google and not in an official doc ? Did you think that Python hadn't documentation ?