python- range()
我是Python的新手,我试图创建一个简单的循环,以便我列出具有Value Integer用户的用户。但是,DB上的用户ID似乎很奇怪(用户1-72的显示正确,距离72是712),因此我需要跳过那些没有任何用户IDS 73-711的用户。必须做什么例外,或者我需要创建一个简单的例外情况?
for iD in range(1, 720):
loginID = iD
for users in tls.getUserByID(loginID):
fname = users.get('firstName')
lname = users.get('lastName')
print(loginID, fname +" "+ lname)
我得到的错误是..
raise testlinkerrors.TLResponseError(
testlink.testlinkerrors.TLResponseError: NO_USER_BY_ID_LOGIN: (getUserByID) - Cannot Find User with DB ID (73)
I'm new to Python, I have tried to create a simple loop in order for me to list users that has userID of value integer. However the userID on the DB seems weird (Users 1-72 are displayed properly, next to 72 is 712 already), so I need to skip those users who does not have any userID 73-711. What exception must be done or do I need to create a simple if else?
for iD in range(1, 720):
loginID = iD
for users in tls.getUserByID(loginID):
fname = users.get('firstName')
lname = users.get('lastName')
print(loginID, fname +" "+ lname)
The error I'm getting is..
raise testlinkerrors.TLResponseError(
testlink.testlinkerrors.TLResponseError: NO_USER_BY_ID_LOGIN: (getUserByID) - Cannot Find User with DB ID (73)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
呼叫
tls.getuserbyid(loginID)
如果没有DB ID的用户,则您将获得异常语句。You will get exception when call
tls.getUserByID(loginID)
if no user with DB ID, so you need to handle the exceptiontestlinkerrors.TLResponseError
bytry/except
statement.您可以尝试此@CKMB
You can try this @ckmb
您可以通过简单的尝试/除外跳过故障。
You can skip the failures with a simple try/except block.
您可以编写一个简单的功能,该功能需要一个值得一提的数字来跳过并返回发电机。
现在:
产量:
You could write a simple function that takes an iterable and a range of numbers to skip and returns a generator.
And now:
Yields:
我已经使用了@Jason Yang的答案 - 看来这是我期望的,但是我相信跳过73-711会增加运行时,如何自动跳过它们?就像这些int不存在于该范围内,然后跳过
I have used the answer of @Jason Yang - and it seems this is what I'm expecting to have, however I believe skipping 73-711 will increase runtime, are there any ways on how to automatically skip them? Like if these int are not present in the range, then skip
有多种方法可以获取某些数字范围的子集。
如果您知道可以使用链
rel 知道不会工作。
您可以像克里斯(Chris)答案一样使用过滤,
您可以使用try-except,而只是处理成功的滤波,
您可以通过记住有问题的滤镜,将其保存在某个地方并将其用于过滤,从而结合上面的两个。对于保存部分,如果我们想在关闭程序后要记住,我们可以使用 json < /a>模块将其保存到
例如“例如
编辑”的文件中:
您还可以将功能概括为将用户和列表忽略以及其他可能需要的内容,并且可以返回沿着它找到的有问题的用户集如何处理适当的信息。
现在,随着函数现在概括,您可以进行不同类型的呼叫,例如
There are a number of way to get some subset of some range of number
If you know the boundaries you can use chain
for your example is
itertools.chain(range(73),range(712,720))
, that way you don't waste time with something you alredy know is not going to work.You can use filtering like in Chris answer
You can use try-except and just processing the successful ones
You can combine the two above by remembering the problematic ones, saving it somewhere, and use that to filter. For the saving part, if we want to remember after closing the program, we can use json module to save it into a file
Like for example
EDIT:
You can also generalize the function to take the user and the list to ignore and whatever else it might need, and it can return the set of problematic user it found along the way and do something appropriate with that info.
Now with the function now generalize like that, you can do different types of call such as