I have the following code which is throwing an error: ValueError: Substring Not Found.
import os, sys
myCwd = os.path.abspath(__file__)
svtestcases = os.path.normpath('Tests/SVTestCases')
tcPath = myCwd[:myCwd.index(svtestcases) + len(svtestcases)]
sys.path.insert(0, tcPath)
The error is raised from the fourth line of the myCwd.index(svtestcases) part.
The path of the python script is : "C:\Netra_Step_2015\Tests\SVTestcases\TC-Regression"
What might be the issue? Also why there is a ':' before myCwd.index? Can anyone explain please?
Looks like your "myCwd" and "svtestcases" doesn't have anything in common and when you try to find the index of substring "svtestcases" , it is not matched at all with your myCwd.
For e.g :
>>> a = '/Test/test1/test2/test3'
>>> a.index('/Test')
0
>>> a.index('test2')
12
>>>
>>> a.index('abc')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>>
Though the comment already mentions about it. i just added a little code to make it more easy to understand. It also tells you about the colon part.
Read more about slicing and you will find it.
Related
I know this is an easy fix, but could someone tell me how to call a python file in the python Console who have this symbol: -.
Here is my mistake:
>>> import main #no error here
>>> import a1-devoir1
File "<input>", line 1
import a1-devoir1
Syntax Error: invalid syntax
You must name your files so that they only contains letters, underscores, or numbers (but not as the first character). All libraries and modules must follow this.
So rename your .py file to a1_devoir and then try import a1_devoir
I'm running this code in Sublime Text 3 with python 3.6 installed.
The Error I get:
Traceback (most recent call last):
File "C:\SublimeCode\Creating list from txt.pyw", line 3, in <module>
List = open('C:\SublimeCode\Employee\EmployeeList.txt').readlines()
FileNotFoundError: [Errno 2] No such file or directory:
'C:\\SublimeCode\\Employee\\EmployeeList.txt'
I have all these folders as listed in the correct places but yet I get the error.
Code:
List = open('C:\SublimeCode\Employee\EmployeeList.txt').readlines()
print(List)
First using \ is a string literal you must use a \\ to escape this property (or you can use / instead) and you should also set a property as well as read the file before attempting to manipulate the contents.
You can set the property using r (in this case)
list_ = open('C:\\SublimeCode\\Employee\\EmployeeList.txt', 'r').readlines()
print(list_)
You should not use capitals in variables only to set constants. Since list is a function you can escape this property by adding a _ after it see pep8 and as described in this post.
I've always used the re module to do things such as re.match and re.sub, the basic stuff, and it's always worked fine for me.
All of a sudden, I'm getting an AttributeError when trying to use basic methods such as match and sub.
Here is some example code I made:
import re
regex = '^[a-z]{3}'
r = re.match(regex, 'asd')
print r
Here's the stacktrace:
Traceback (most recent call last):
File "te.py", line 4, in <module>
r = re.match(regex, 'asd')
AttributeError: module 're' has no attribute 'match'
I've never had problems with the module. I tried in both python 2.x and 3, same error. I'm not very knowledgeable about how imports work, so this is likely a simple mistake by me.
Thanks
Delete your re.py file in the same directory as the te.py file. You commited a typo while naming test files. Your error points that your current file is named te.py, and since t is close to r in the keyboard, this might explain everything.
Just to prove my curiosity I created an empty re.py file in the same directory as te.py, which holds your code. And I got the same error as you did.
My guess is that you're getting something you don't expect for the re module that you're importing.
Maybe try this:
import re
print re.__file__
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.pyc
And see if the result you get is sensible.
So I'm trying to get to grips with using the rpy2 module (I am familiar with R but new to Python). Following this tutorial, I first load the library and assign it to the variable 'r' using:
import rpy2
import rpy2.robjects as robjects
r = robjects.r
then I try to perform a simple operation to confirm everything is working:
print(r[2+2])
but I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\rpy2\robjects\__init__.py", line 248, in _
_getitem__
res = _globalenv.get(item)
TypeError: argument 1 must be str, not int
I'm sure it's just something stupid I'm doing wrong, but any advice would be much appreciated. I'm using python3.4.2 (64bit), rpy2-2.5.6 (64bit) on a Windows 7 machine (64bit).
You should use print(r(2+2)) instead of print(r[2+2]).
When you use r[2+2] you are trying to recover an element corresponding to the index 4 (the result of 2+2) of the r iterable. And your r object doesn't seem to respond to this kind of message.
Ok I think I have figured it out. For R to evaluate the function inside the parenthesis, the function must be in quotes e.g.
r("2+2")
This is what was confusing me because this looks like I'm providing a string.
Oddly I don't print the result (4) by using:
print(r("2+2"))
as this prints:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
print(r("2+2"))
File "C:\Python34\lib\site-packages\rpy2\robjects\robject.py", line 49, in __str__
s = str.join(os.linesep, s)
TypeError: sequence item 0: expected str instance, bytes found
Instead I just print the result using:
answer = r("2+2")
answer[0]
(Because R is vector based, the initial value of the vector is the answer so you have to index it at the first position, otherwise you get:
answer = r("2+2")
answer
<FloatVector - Python:0x0000000005836EC8 / R:0x00000000047A51A0>
[4.000000]
Thanks for you help
Hefin
I was following the tutorial on the webpage:
http://pythonhosted.org/bioservices/compound_tutorial.html
Everything worked well until I reached the following command:
uni = UniChem()
and then I received the error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "P:\Anaconda\lib\site-packages\bioservices\unichem.py", line 84, in __init__
maxid_service = int(self.get_all_src_ids()[-1]['src_id'])
TypeError: list indices must be integers, not str
As a minimum working example:
from bioservices import *
uni = UniChem()
and then I receive the error. I understand the error (for the most part) but I don't know how to fix it. So my question is how do I fix the function or work around it?
The overall aim it to map a list of 1000 drug names (and hopefully more in the near future) to Chembl IDs.
The error you saw is probably related to the fact that when you tried to connect to UniChem service, it was off for maintenance or it took too much time to initialize. The consequence is that the service was not started hence the error message you got.
I've just tried (bioservices 1.2.6)
from bioservices import *
uni = UniChem()
and it worked. The following request also worked:
>>> mapping = uni.get_mapping("kegg_ligand", "chembl")
'CHEMBL278315'