how to pass date value into my url using python - python-3.x

htmlfile=urllib.request.urlopen("https://hermes.goibibo.com/hotels/v2/search/data/v3/6771549831164675055/{pickUpDate}/{dropOffDate}/1-1_0?s=popularity&cur=INR&f={}&pid=0".format(pickUpDate=pickUpDate, dropOffDate=dropOffDate))

You have three {} pairs but 2 values in your URL. You need to match the {} pairs with the given values.
For instance:
"{v1} is {v2}. {v3}".format(v1="Cat", v2="Animal", v3="Absolutely!")
the string is "Cat is Animal. Absolutely!"

At the end of your string you have "..INR&f={}&pid=0".format().
If this is not a placeholder into which you want to place text via .format(), then change it to have double moustache brackets. i.e:
"...INR&f={{}}&pid=0".format()
This will tell .format() that you really just want the brackets to exist there as a string
So, in general:
>>"{x}: {}".format(x="Hello")
IndexError: tuple index out of range
but
>>"{x}: {{}}".format(x="Hello")
'Hello: {}'

htmlfile=urllib.request.urlopen("https://hermes.goibibo.com/hotels/v2/search/data/v3/6771549831164675055/"+pickUpDate+"/"+dropOffDate+"/1-1_0?s=popularity&cur=INR&f={}&pid=0")

Related

Add single quotes around string in python

I have converted list to string.
but after conversion I am getting string without single quote around the string
for eg:
items = ['aa','bb','cc']
items = ','.join(items)
output is : aa,bb,cc
expected output: 'aa','bb','cc'
You could use a list comprehension to quote the individual strings in the list:
items = ['aa','bb','cc']
items = ','.join([f"'{i}'" for i in items])
print(items) # 'aa','bb','cc'
One way to accomplish this is by passing the list into a string formatter, which will place the outer quotes around each list element. The list is mapped to the formatter, then joined, as you have shown.
For example:
','.join(map("'{}'".format, items))
Output:
"'aa','bb','cc'"

Replace a specific value in a string in python

I'm trying to replace a specific values in a long string. Is it possible to do this with replace function in python?
a snipit of the string is:
'rh':0, 'rp':0, 't':'b.nan','rh':1, 'rp':1, 't':'b.nan'
my snipit string should look like
'rh':0, 'rp':0, 't':b.nan,'rh':1, 'rp':1, 't':b.nan
i'm trying to replace the 'b.nan' to b.nan but it doesn't work.
The code i'm using:
a.replace("'b.nan'", "b.nan")
You can index strings like arrays:
string = "hello"
print(string[1])
this prints 'e'
You could try finding the index and then replacing it as such

Find index of a specific character in a string then parse the string

I have strings which looks like this [NAME LASTNAME/NAME.LAST#emailaddress/123456678]. What I want to do is parse strings which have the same format as shown above so I only get NAME LASTNAME. My psuedo idea is find the index of the first instance of /, then strip from index 1 to that index of / we found. I want this as a VBScript.
Your way should work. You can also Split() your string on / and just grab the first element of the resulting array:
Const SOME_STRING = "John Doe/John.Doe#example.com/12345678"
WScript.Echo Split(SOME_STRING, "/")(0)
Output:
John Doe
Edit, with respect to comments.
If your string contains the [, you can still Split(). Just use Mid() to grab the first element starting at character position 2:
Const SOME_STRING = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(Split(SOME_STRING, "/")(0), 2)
Your idea is good here, you should also need to grab index for "[".This will make script robust and flexible here.Below code will always return strings placed between first occurrence of "[" and "/".
var = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(var, (InStr(var,"[")+1),InStr(var,"/")-InStr(var,"[")-1)

rstrip() has no effect on string

Trying to use rstrip() at its most basic level, but it does not seem to have any effect at all.
For example:
string1='text&moretext'
string2=string1.rstrip('&')
print(string2)
Desired Result:
text
Actual Result:
text&moretext
Using Python 3, PyScripter
What am I missing?
someString.rstrip(c) removes all occurences of c at the end of the string. Thus, for example
'text&&&&'.rstrip('&') = 'text'
Perhaps you want
'&'.join(string1.split('&')[:-1])
This splits the string on the delimiter "&" into a list of strings, removes the last one, and joins them again, using the delimiter "&". Thus, for example
'&'.join('Hello&World'.split('&')[:-1]) = 'Hello'
'&'.join('Hello&Python&World'.split('&')[:-1]) = 'Hello&Python'

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