我读到这里,它是推荐使用with open(filename),而不是使用对open(filename)和filename.close()(至少对基本任务),并且它也是更好地使用try.
Q1:如果我的理解是正确的,那么正确的顺序是什么?
try:
with open(filename) as f:
do something, eg. match string
Run Code Online (Sandbox Code Playgroud)
要么
with open(filename) as f:
try:
do something, eg. match string
Run Code Online (Sandbox Code Playgroud)
Q2:如果我还想插入一些代码来弹出文件处理错误的弹出窗口,哪种情况更好?(找不到文件,或者其他东西)
我有一个字节列表,想要获取最后一项,同时保留其字节类型。使用 [-1] 会给出 int 类型,因此这不是直接的解决方案。
示例代码:
x = b'\x41\x42\x43'
y = x[-1]
print (y, type(y))
# outputs:
67 <class 'int'>
Run Code Online (Sandbox Code Playgroud)
对于任意索引我知道该怎么做:
x = b'\x41\x42\x43'
i = 2 # assume here a valid index in reference to list length
y = x[i:i+1]
print (y, type(y))
# outputs:
b'C' <class 'bytes'>
Run Code Online (Sandbox Code Playgroud)
也许我可以计算列表长度,然后指向一个绝对长度为 1 的数字,而不是相对于列表末尾的数字。
但是,有没有更优雅的方法来做到这一点?(即类似于简单的[-1])我也无法想象如何从列表末尾反向适应[i:i+1]原则。
我有一个串行 Python 程序,Linux 环境(Raspbian / Raspberry Pi),它通过 USB 到串行适配器使用串行端口。我需要处理用户拔下 USB 适配器然后重新插入的情况。
问题是,重新连接时,ttyUSB0变为ttyUSB1,因此不再找到该端口。但是,如果我停止 Python 程序(键盘中断)并再次拔下并重新插入 USB 适配器,则端口会返回到ttyUSB0(这样我就可以重新开始)。这只会在 Python 程序停止时发生。
我在触发器模式下测试了该程序(它似乎正在工作)以便在不再找到ttyUSB0时使用ttyUSB1,反之亦然,如果不再找到ttyUSB1,请使用ttyUSB0,等等,但是这个对我来说似乎是一个奇怪的解决方案。
有没有更好的方法来强制 pySerial “忘记”它曾经连接到 ttyUSB0 以防出错,并在程序仍在运行时将当前端口释放到系统?
这是一个有效的触发器测试程序:
import serial
import time
p = "/dev/ttyUSB0"
while True:
error_flag = False
try:
s = serial.Serial(port=p, baudrate=9600, bytesize=8, parity="N", stopbits=1, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None)
except Exception as e:
error_flag = True
if "ttyUSB0" in str(e):
p = "/dev/ttyUSB1"
print …Run Code Online (Sandbox Code Playgroud) 试图在我的Python代码上运行mki18n.py脚本,我收到一条警告:
>>> dlg = wx.MessageDialog(None, str (_("Attached device is \"%s\",\nschedule file header is for \"%s\"") % (rt_model, line)), _("Device mismatch"), wx.OK | wx.ICON_ERROR)
Run Code Online (Sandbox Code Playgroud)
然后给出这个:
warning: 'msgid' format string with unnamed arguments cannot be properly localized:
The translator cannot reorder the arguments.
Please consider using a format string with named arguments,
and a mapping instead of a tuple for the arguments.
Run Code Online (Sandbox Code Playgroud)
mki18n.py脚本不喜欢两个连续的存在s%,但我无法解码警告信息所说的内容.否则(运行我的程序而不关心i18n)我没有错误,并且该对话框始终显示正常.
那条线有什么问题?(有什么可以改进的?)
编辑通过使用geni18n.py(及其来自i18nwxapp包的相关文件),我得到了没有错误的预期结果(即生成.pot文件进行翻译).我仍然不知道我的代码中是否存在geni18n容忍的问题,或者mki18n是否存在由我的特定代码行触发的问题(?).
我有这个代码,用于在20x2 LCD显示屏上显示一些文字:
#!/usr/bin/python
LCDCHARS = 20
LCDLINES = 2
def WriteLCD(text_per_LCD):
chunked = (text_per_LCD[i:LCDCHARS+i] for i in range (0, len(text_per_LCD), LCDCHARS))
count_l = 0
for text_per_line in chunked:
# print will be replaced by actual LCD call
print (text_per_line)
count_l += 1
if count_l >= LCDLINES:
# agree to lose any extra lines
break
WriteLCD("This text will display on %s LCD lines" % (LCDLINES))
Run Code Online (Sandbox Code Playgroud)
将输出示例字符串
This text will displ
ay on 2 LCD lines
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能分割字符串而不会破坏字词?即使第二行变得更长并且不显示.
我在javascript部分读了一个类似的问题,在ruby部分读了另一个,但是我无法将给定的答案翻译成我的Python案例.