This question already has answers here:
What does a backslash by itself ('\') mean in Python? [duplicate]
(5 answers)
What is the purpose of a backslash at the end of a line?
(2 answers)
Closed 1 year ago.
While I was searching code from the internet about YouTube data analysis, I found code like this:
df_rgb2['total_sign_comment_ratio'] = df_rgb2['total_number_of_sign'] / df_rgb2['comment_count']
total_sign_comment_ratio_max = df_rgb2['total_sign_comment_ratio'].replace([np.inf, -np.inf], 0).max()
df_rgb2['total_sign_comment_ratio'] = \
df_rgb2['total_sign_comment_ratio'].replace([np.inf, -np.inf], total_sign_comment_ratio_max*1.5)
and I was wondering why the analyst used the expression:
df_rgb2['total_sign_comment_ratio'] = \
because whether I apply that code or not, the result is same.
I tried to find the meaning of '\' but all I have got is how to use '\' when printing out the result.
\ is usually used to make a piece of code go on onto multiple lines. If you where to just press enter and continue to write code a line below for example declaring a variable, it would count as an error.
You use this when you need to tidy up code or when your working window is too small for some reason.
See: https://developer.rhino3d.com/guides/rhinopython/python-statements/
Related
This question already has answers here:
How do I split the definition of a long string over multiple lines?
(30 answers)
How can I split up a long f-string in Python?
(2 answers)
Closed 10 months ago.
I'm building a rather long file path, like so:
file_path = f"{ENV_VAR}/my_dir/{foo['a']}/{foo['b']}/{bar.date()}/{foo['c']}.json"
This is a simplified example. The actual path is much longer.
To make this line shorter and more readable in code, I have tried the following:
file_path = f"{ENV_VAR}/my_dir\
/{foo['a']}\
/{foo['b']}\
/{bar.date()}\
/{foo['c']}.json"
This works but also affects the actual string in my program.
More specifically, the linebreaks are added to the string value itself, which is undesirable in this case. I only want to change the formatting of the source code.
Is it possible to format the string without affecting the actual value in my program?
This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 1 year ago.
enter image description here
is there a way to print single backslash within list?
Regarding the first version of your question, I wrote this:
First, this expression x='\' isn't right in Python in python. you should rather puth it this way: x='\\', since back slash is a special character in python.
Second, try this:
l=['\\'] print(l)
This will print: ['\\']
But when you execute this: print(l[0]), it renders this '\'. So basically, this ['\\'] is the way to print a backslash within a list.
This question already has answers here:
What do backticks mean to the Python interpreter? Example: `num`
(3 answers)
Meaning of the backtick character in Python
(2 answers)
Closed 1 year ago.
Lots of old python code I look in has this ` symbol around a lot of stuff, what does it do? Now it is not considered valid syntax, obviously.
And I don't think it is just another string identifier, its sometimes wrapped around functions in the code I'm looking at.
Any help will be appreciated.
This question already has answers here:
How to grab number after word in python
(4 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 2 years ago.
Hey I need to search for variable data in a console from a page source
The data will be shown like this:
"data":[13,17]
It will vary a lot with the amount of units inside the table. I have tried out several RE expressions, but the closest I have come to a result, is with a fixed amount of units.
self.driver.get("website.com")
apidata = self.driver.page_source
print(apidata)
datasetbasic = re.search('"data":[[0-99,0-99]+', apidata)
print(datasetbasic)
Instead of having it as a fixed amount, how do I capture anything that is inside the data table?
Before you ask, I cannot use xpath or any other selenium calls to capture this data directly from the webpage (I think), because the element is from a graph, where the data is only visible in the actual console.
Any help is appreciated
This question already has answers here:
Variable interpolation in Python [duplicate]
(5 answers)
Closed 4 years ago.
I'm pretty novice in Python scripting and was trying to run an API call with some dynamic values passed.
A simple idea about the code is, it gets two datetimes in epoch(10 second interval)and calls an API to do a function.
import commands
end = str(datetime.datetime.now().strftime("%s"))
start = str((datetime.datetime.now() - datetime.timedelta(seconds=10)).strftime("%s"))
output = commands.getstatusoutput("curl 'http://my-api-url/object?param1=1&start=$start&end=$end&function=average'")
It doesn't work as the variables start and end are not getting expanded/substituted.
As you see, I come from bash scripting and tried looking on several variable substitution commands from web, but nothing specific found to my case here.
Use str.format
Ex:
import commands
end = str(datetime.datetime.now().strftime("%s"))
start = str((datetime.datetime.now() - datetime.timedelta(seconds=10)).strftime("%s"))
output = commands.getstatusoutput("curl 'http://my-api-url/object?param1=1&start={start}&end={end}&function=average'".format(start=start, end=end))
In Python, you can concatenate strings using the '+' operator.
In your case you could write:
output = commands.getstatusoutput("curl 'http://my-api-url/object?param1=1&start=" + start + "&end=" + end + "&function=average'")