How to use variable in url? - python-3.x

I'm trying to develop a maco in python. I need a method that changes the URI given in the input variables.
For instance, in www.(variable).com I need to change the URL www.1.com to www.2.com

You can use formatted strings like this:
variable = 1
url = f"www.{variable}.com"

You could store your list of names in a list and then interpolate the variable into the f-string like:
names = ['google', 'amazon', 'microsoft']
for name in names:
print(f"www.{name}.com")
OUTPUT
www.google.com
www.amazon.com
www.microsoft.com

Related

Create some if from list of string / Python3

First sorry for my english...
I need your help to fix a difficult. I have a python script to check some parameters, with this parameters I check some conditions but I would like to add the possibility for the user to create an another check from a string...
To give you an exemple, I will save some coditions in string than I will split to have the check name, limit value,delay,condition (<,>,=...). And from this list (string splited) I woud like to create an "If 'value' 'condition' 'limit value' " but I don't find how to write this IF from my list.
parameter="O2_alert,O2,0.015,<"
para1=parameter.split(',') # ==> para1 =["O2_alert","O2","0.015","<"]
alert_name=para1[0]
value_name=para1[1] # it's a name of a variable in my script not a value
limit_value=para1[2]
condition=para1[3]
# And I woul like to have
if value_name condition limit_value :
Thanks for your help.
You can use like this,
parameter="02_alert,2,0.015,<"
para1=parameter.split(',') # ==> para1 =["02_alert","02","0.015","<"]
alert_name=para1[0]
value_name=para1[1] # it's a name of a variable in my script no a value
limit_value=para1[2]
condition=para1[3]
if eval(f"{value_name} {condition} {limit_value}"):
print('abcd')
It works for you.

How can I change a variable in a .txt template, if I have more than 5000 different values for this variable in an excel table?

I have an excel table with a column called 'interface', and what I want to do is a code that pull each interface value : Port-channel47, Port-channel46,etc... and put it in my .txt template replacing the {interface} part that i have in my txt template.
the value I want to change in the .txt template is "{interface}"
I tried this code:
but I get lost when i want to pull the data.
Anyone can help me? thank you so much in advance
Use string template:
from string import Template
with open('template.txt') as fp:
template = Template(fp.read())
for interface, group in df.groupby('Interface'):
file_name = interface.replace('/', '_') + '_output.txt'
with open(file_name, 'w') as fp:
content = template.substitute(interface=interface)
fp.write(content)
When you iterate over a DataFrameGroupBy object, it returns a 2-tuple containing the key of the group, and all rows matching that key.
A second thing to worry about is that / is not a valid path under most OSes I know so you must replace it with some other character (I chose a _).

Finding substring within string

I want to find a specific string within a string.
For example, let's say I have the string
string = "username:quantopia;password:blabla
How can I then find quantopia?
I am using python 3.
Update: I am sorry I did not mention what I try before..
string.split('username:',1)[1].split(';',1)[0]
But this look very bad and not efficient, I was hoping for something better.
Just use regex as such:
import re
username = re.search("username:(.*);password", "username:quantopia;password:blabla").group(1)
print("username:", username)
This will output quantopia.
In this expression "username:(.*);password" you are saying "give me everything from username: to ;password" So this is why you're getting quantopia. This might as well be ":(.*);" as it will output the same thing in this case.
The simple solution is:
string = "username:quantopia;password:blabla"
username = "username"
if username in string:
# do work.
You might be better to just use split to create a dictionary so you dont need to use multiple regex to extract different parts of data sets. The below will split stirng into key value pairs then split key value pairs then pass the list of lists to dict to create a dictionary.
string = "username:quantopia;password:blabla"
data = dict([pairs.split(':') for pairs in string.split(';')])
print(f'username is "{data["username"]}" and password is "{data["password"]}"')
OUTPUT
username is "quantopia" and password is "blabla"

Struct name from variable in Matlab

I have created a structure containing a few different fields. The fields contain data from a number of different subjects/participants.
At the beginning of the script I prompt the user to enter the "Subject number" like so:
prompt='Enter the subject number in the format SUB_n: ';
SUB=input(prompt,'s');
Example SUB_34 for the 34th subject.
I want to then name my structure such that it contains this string... i.e. I want the name of my structure to be SUB_34, e.g. SUB_34.field1. But I don't know how to do this.
I know that you can assign strings to a specific field name for example for structure S if I want field1 to be called z then
S=struct;
field1='z';
S.(field1);
works but it does not work for the structure name.
Can anyone help?
Thanks
Rather than creating structures named SUB_34 I would strongly recommend just using an array of structures instead and having the user simply input the subject number.
number = input('Subject Number')
S(number) = data_struct
Then you could simply find it again using:
subject = S(number);
If you really insist on it, you could use the method proposed in the comment by #Sembei using eval to get the struct. You really should not do this though
S = eval([SUB, ';']);
Or to set the structure
eval([SUB, ' = mydata;']);
One (of many) reasons not to do this is that I could enter the following at your prompt:
>> prompt = 'Enter the subject number in the format SUB_n: ';
>> SUB = input(prompt, 's');
>> eval([SUB, ' = mydata;']);
And I enter:
clear all; SUB_34
This would have the unforeseen consequence that it would remove all of your data since eval evaluates the input string as a command. Using eval on user input assumes that the user is never going to ever write something malformed or malicious, accidentally or otherwise.

Matlab: Remove fields with similar string names in a single command

So I have a structure, r, that contains multiple headers of the form:
Header_0001
Header_0002
Header_0003, and so on whose names are represented as strings.
Is there a way to format the strings so that I can remove these headers with a single command?
i.e.
r=rmfield(r,Header_00XX)
where X can be any number. I have tried using wildcards, anchors, etc. but have not found a method that works as of yet.
Try this:
fields = fieldnames(r);
r = rmfield(r, fields(find(~cellfun(#isempty,strfind(fields, 'Header_00')))))

Resources