这里:
from os.path import exists as foo
print foo.__name__
Run Code Online (Sandbox Code Playgroud)
我们得到:'exists'.为什么不'foo'呢?哪个属性会给出'foo'?
我想很好地捕获“找不到主机 *** 的主机密钥”时的错误,并向最终用户提供适当的消息。我试过这个:
import pysftp, paramiko
try:
with pysftp.Connection('1.2.3.4', username='root', password='') as sftp:
sftp.listdir()
except paramiko.ssh_exception.SSHException as e:
print('SSH error, you need to add the public key of your remote in your local known_hosts file first.', e)
Run Code Online (Sandbox Code Playgroud)
但不幸的是输出不是很好:
SSH error, you need to add the public key of your remote in your local known_hosts file first. No hostkey for host 1.2.3.4 found.
Exception ignored in: <function Connection.__del__ at 0x00000000036B6D38>
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\pysftp\__init__.py", line 1013, …Run Code Online (Sandbox Code Playgroud) 是否可以直接从 HTML 标签传递变量<script>:
<script async src="https://example.com/lib.js" site="test.com"></script>
Run Code Online (Sandbox Code Playgroud)
这样lib.js可以site像常规变量一样访问?
我使用python3.9制作了一个独立的软件,它可以在我的系统和另一个 Windows 10 系统上正常运行,但我尝试在Windows 7 Ultimate上运行该软件,但它显示了一些错误。请注意,我通过使用PyInstaller绑定所有必需的 python 模块来制作该软件。这些错误如下:
我该如何在 Windows 7 上运行该程序?
可以使用Google的语音识别API通过执行请求来获取音频文件(WAV,MP3等)的转录 http://www.google.com/speech-api/v2/recognize?...
示例:我在WAV文件中说过" 一二三五 ".谷歌API给了我这个:
{
u'alternative':
[
{u'transcript': u'12345'},
{u'transcript': u'1 2 3 4 5'},
{u'transcript': u'one two three four five'}
],
u'final': True
}
Run Code Online (Sandbox Code Playgroud)
问题:是否可以获得每个单词的时间(以秒为单位)?
用我的例子:
['one', 0.23, 0.80], ['two', 1.03, 1.45], ['three', 1.79, 2.35], etc.
Run Code Online (Sandbox Code Playgroud)
即,
在时间00:00:00.23和00:00:00.80之间已经说过"一个"字样,在时间00:00:01.03和00:00:01.45(以秒为单位)之间说出了"两个"字样.
PS:寻找支持除英语之外的其他语言的API,尤其是法语.
audio speech-recognition speech speech-to-text google-speech-api
我已阅读在 Cython 中制作可执行文件和 BuvinJ 对如何有效地混淆 Python 代码的回答?并想测试编译后用 Cython 编译的源代码是否真的“不再存在”。确实流行使用 Cython 是一种保护 Python 源代码的方法,例如参见文章Protecting Python Sources With Cython。
让我们以这个简单的例子为例test.pyx:
import json, time # this will allow to see what happens when we import a library
print(json.dumps({'key': 'hello world'}))
time.sleep(3)
print(1/0) # division error!
Run Code Online (Sandbox Code Playgroud)
然后让我们使用 Cython:
cython test.pyx --embed
Run Code Online (Sandbox Code Playgroud)
这会产生一个test.c. 让我们编译它:
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
cl test.c /I C:\Python37\include /link C:\Python37\libs\python37.lib
Run Code Online (Sandbox Code Playgroud)
有用!它生成了一个 140KB 的test.exe可执行文件,不错! …
读取CSV时,不是跳过第一行(标题),而是按编号读取行项:
with open('info.csv') as f:
reader = csv.reader(f, delimiter=';')
next(reader, None)
for row in reader:
name = row[0]
blah = row[1]
Run Code Online (Sandbox Code Playgroud)
是否有通过使用标题名称访问行项目的内置方法?就像是:
with open('info.csv') as f:
reader = csv.reader(f, delimiter=';', useheader=True)
for row in reader:
name = row['name']
blah = row['blah']
Run Code Online (Sandbox Code Playgroud)
哪里info.csv有一个标题行:
名字; blah
John; Hello2
Mike; Hello2
Tkinter的canvas小部件具有内置功能:
移动/与平移画布(例如用点击+拖拽)canvas.scan_mark和canvas.scan_dragto,看到这个问题
缩放画布上的矢量元素canvas.scale,但遗憾的是,这不适用于画布上的位图图像
幸运的是,此方法允许缩放图像(通过手动重绘图像的缩放部分).但:
当我们重新绘制画布的特定部分时,移动/平移功能将不再起作用...
我们绝对需要渲染超过当前显示的区域,以允许移动/平移.假设我们在画布上有1000x1000位图,我们想要缩放50倍...如何避免在内存中有50.000 x 50.000像素的位图?(RAM中2.5千兆像素太大).我们可以考虑仅渲染视口,或者比当前视口稍微多一点以允许平移,但是一旦平移导致渲染区域的边缘怎么办?
如何在Tkinter画布上使用移动/平移+缩放功能,适用于图像?
以下代码允许在单击时获取坐标.但是如何使用Google Maps API在地图上点击地址或城市名称或地区名称或国家/地区?
var myLatlng = new google.maps.LatLng(41.38,2.18);
var myOptions = { zoom: 13, center: myLatlng}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
google.maps.event.addListener(map, 'click', function(event) {alert(event.latLng);});Run Code Online (Sandbox Code Playgroud)
html, body, #map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}Run Code Online (Sandbox Code Playgroud)
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script>
<div id="map-canvas"></div>Run Code Online (Sandbox Code Playgroud)
我想读取来自 Gmail 备份的 3GB .mbox 大文件。这有效:
import mailbox
mbox = mailbox.mbox(r"D:\All mail Including Spam and Trash.mbox")
for i, message in enumerate(mbox):
print("from :",message['from'])
print("subject:",message['subject'])
if message.is_multipart():
content = ''.join(part.get_payload(decode=True) for part in message.get_payload())
else:
content = message.get_payload(decode=True)
print("content:",content)
print("**************************************")
if i == 10:
break
Run Code Online (Sandbox Code Playgroud)
但仅前 10 条消息就需要 40 秒以上。
有没有更快的方法来使用 Python 访问大的 .mbox 文件?