Using Python 2.7 I am trying to put a variable inside a (' ')
Orignial string
r = search.query('Hello World')
What I have tried so far and is not working...
r = search.query('{0}').format(company_name_search)
r = search.query('s%') % (company_name_search)
Both methods are not working and output the error below. What is the best way to take r = search.query('Hello World') and put a variable for Hello World so I can dynamically change the search parameters at will?
AttributeError: 'Results' object has no attribute 'format'
TypeError: unsupported operand type(s) for %: 'Results' and 'str'
You are now applying the .format() or % to the query(). Apply them to the string. '{0}'.format(company_name_search) or '%s'%(company_name_search). Then throw that into the query function, like
r = search.query('{0}'.format(company_name_search))
r = search.query('s%' % (company_name_search))
Related
def function1(self,*windows):
xxx_per_win = [[] for _ in windows]
for i in range(max(windows),self.file.shape[0]):
for j in range(len(windows)):
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
...
...
...
o = classX(file)
windows = [3,10,20,40,80]
output = o.function1(windows)
If I run the code above it says:
for i in range(max(windows),self.file.shape[0]):
TypeError: 'list' object cannot be interpreted as an integer
and:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
TypeError: unsupported operand type(s) for -: 'int' and 'list'
This problem only occurs when windows is variable length (ie *windows and not just windows).
How do I fix this? What is causing this?
The max function expects to be passed multiple arguments, not a tuple containing multiple arguments. Your windows variable is a tuple.
So, not:
for i in range(max(windows),self.file.shape[0]):
Do this instead:
for i in range(max(*windows),self.file.shape[0]):
On your second error, involving the line:
zz = self.file['temp'][i-windows[j]:i].quantile(0.25)
# TypeError: unsupported operand type(s) for -: 'int' and 'list'
Ok, you are subtracting, and it's complaining that you can't subtract a list from an integer. And since I have no idea what windows[j] contains, I can't say whether there's a list in there or not.. but if there is, there can't be. You haven't given us a working example to try.
What I suggest you do is to put some debugging output in your code, eg:
print('i=%s, j=%s, windows[j]=%s' % (i, j, windows[j]))
and thus see what your data looks like.
This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Why does the print function return None?
(1 answer)
Closed 6 months ago.
I have code for concatenation of two string but it is showing me an error.
Here is the code :
Name = "Praveen kumar"
print (Name)+"Good boy"
Error message : unsupported operand type(s) for +: 'NoneType' and 'str'
How can I fix this?
print is a function, returning None.
So when you write
print(Name) + "Good boy"
You are actually adding the return value of the function call (i.e. None) to the string.
What you wanted instead was probably:
print(Name, "Good boy")
You are printing Name and then adding the string Good boy to it, you need to enclose your addition within the function call.
print(Name) will return None (it is a function which defines no return value) which is why you're getting the unsupported operand... error.
The code below will achieve what you want.
Name = "Praveen kumar"
print(Name + "Good boy")
However note that there will be no space between Name and 'Good boy'. If you want a space then you can use print(Name, "Good boy") as the default separator is sep = ' ', meaning that a space will be added between your arguments.
I have the following cmd,am trying to equate data in var including quotes to changes('changes=var' as shown below),can anyone suggest the syntax to do it?
var = "769373 769374"
cmd = ['tool', '--server=commander.company.com', 'runProcedure', 'Android_Main',
'--procedureName', 'priority_kw', '--actualParameter',
`'changes=var'`,
'gerrit_server=review-android.company.com']
Use + to concatenate in Python. Example below show how it's used:
cmd = ['tool', '--server=commander.company.com', 'runProcedure', 'Android_Main',
'--procedureName', 'priority_kw', '--actualParameter',
'changes=' + var, 'gerrit_server=review-android.company.com']
I would recommend doing it this way:
var = "769373 769374"
cmd = ['tool', '--server=commander.company.com', 'runProcedure', 'Android_Main',
'--procedureName', 'priority_kw', '--actualParameter',
'changes={}'.format(var),
'gerrit_server=review-android.company.com']
Using string concatenation ('changes=' + var) works fine in this case, but that approach will sometimes fail when you're not expecting it to. For example, if var was an int, you'd get a TypeError: cannot concatenate 'str' and 'int' objects.
I get this error when trying to convert possible integer variables:
for page in domain.page_set.all():
filename = str(domain.url) + '_page_' +str(page.id())+ '.html'
The error:
File "/Applications/djangostack-1.4.7-0/apps/django/django_projects/controls/polls/models.py", line 40, in make_config_file
filename = str(domain.url)+"_page_"+str(page.id())+".html"
TypeError: 'long' object is not callable
What is wrong here? what does "long is not callable" means?
page.id is a long, which is not a function, and thus not callable:
In [1]: id = 5586L
In [2]: type(id)
Out[2]: long
In [3]: id()
TypeError: 'long' object is not callable
Try just doing str(page.id).
Alternatively, you could use Python's string formatting like so:
for page in domain.page_set.all():
filename = "{}_page_{}.html".format(domain.url, page.id)
a'$'
money=1000000;
portfolio=0;
value=0;
value=(yahoostock.get_price('RIL.BO'));
portfolio=(16*(value));
print id(portfolio);
print id(value);
money= (money-portfolio);
'''
I am getting the error:
Traceback (most recent call last):
File "/home/dee/dee.py", line 12, in <module>
money= (value-portfolio);
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Since money is integer and so is portfolio, I cant solve this problem..anyone can help???
value=(yahoostock.get_price('RIL.BO'));
Apparently returns a string not a number. Convert it to a number:
value=int(yahoostock.get_price('RIL.BO'));
Also the signal-to-noise ratio isn't very high. You've lots of (,), and ; you don't need. You assign variable only to replace them on the next line. You can make your code nicer like so:
money = 1000000
value = int(yahoostock.get_price('RIL.BO'));
portfolio = 16 * value;
print id(portfolio);
print id(value);
money -= portfolio;
money and portfolio are apparently strings, so cast them to ints:
money= int( float(money)-float(portfolio) )
As the error message clearly states, both are string, cast with int(var).
Note:
Let's see what can we decude from the error message:
portfolio must be string(str), which means value is also a string. Like this:
>>> 16*"a"
'aaaaaaaaaaaaaaaa'
and apparently you missed to post relevant code because the error message tells you that money is str as well.
I think the problem here is assuming that because you have initialised variables with integer values they will remain as integers. Python doesn't work this way. Assigning a value with = only binds the name to the value without paying any attention to type. For example:
a = 1 # a is an int
a = "Spam!" # a is now a str
I assume yahoostock.getprice(), like many functions that get data from websites, returns a string. You need to convert this using int() before doing your maths.