首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bbc-microbit: micropython AttributeError:'str‘对象没有属性'partition’

bbc-microbit: micropython AttributeError:'str‘对象没有属性'partition’
EN

Stack Overflow用户
提问于 2019-02-03 23:41:18
回答 1查看 302关注 0票数 1

在BBC的微位上,我得到了这个错误,我不知道为什么:

代码语言:javascript
复制
AttributeError: 'str' object has no attribute 'partition'

运行此代码时:

代码语言:javascript
复制
uart.write('Received: "' + incoming + '"\n')
head, mid, tail = incoming.partition(' ')

传入是一个字符串,可以在控制台中看到

代码语言:javascript
复制
MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822
Type "help()" for more information.
>>> 
>>> Received: "buggy direction 2.16 1.2"
Traceback (most recent call last):
  File "__main__", line 122, in <module>
  File "__main__", line 25, in drive
AttributeError: 'str' object has no attribute 'partition' 

你知道这里能做些什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-06 03:06:36

BBC micropython字符串类没有分区方法。尝试使用split方法。示例中的字符串"buggy direction2.161.2“有四个元素。尝试将其仅拆分为三个变量head、mid、tail将导致错误。

您可以使用下面的示例代码访问字符串的第一个和最后一个元素:

代码语言:javascript
复制
words = incoming.split(',')
head = words[0]
tail = words[-1]
print('head: {} tail: {}'.format(head, tail))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54504534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档