我有一个在 Windows 上用 Python 3.7.4 编写的程序,可以在浏览器中打开网页。它显示默认浏览器,并让用户有机会更改他们想要用来打开程序的浏览器。
使用此技术初始化程序时会检测到默认浏览器:
from winreg import HKEY_CURRENT_USER, OpenKey, QueryValueEx
import webbroswer
reg_path = r'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice'
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
firefox_path = 'C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s'
with OpenKey(HKEY_CURRENT_USER, reg_path) as key:
print(QueryValueEx(key, 'ProgId'))
thedefaultbrowser = (QueryValueEx(key, 'ProgId'))
thedefaultbrowser = (thedefaultbrowser[0])
thedefaultbrowser = (thedefaultbrowser[0:2])
if thedefaultbrowser == "Fi":
browser_path = firefox_path
if thedefaultbrowser == "Ch":
browser_path = chrome_path
Run Code Online (Sandbox Code Playgroud)
链接是这样打开的,作为程序后面发生的无限循环的一部分:
while True:
# [lots of GUI code that isn't relevant]
event = input()
if event …Run Code Online (Sandbox Code Playgroud) 我想让 Python 3.7.1 选择一个 0 到 100 之间的数字。但是我希望较低的数字比较高的数字更有可能,以反向指数平滑分级曲线的方式(没有准确地说)。
我想我可以从
myrandomnumber = random.randint(0, 100)
Run Code Online (Sandbox Code Playgroud)
然后将其链接到某种数组以确定每个数字的不同百分比。我见过其他人用随机骰子来做到这一点,但问题是,这对于六种可能性来说是相当整洁的,我想对一百个(或更多)这样做,并且不想坐在那里做一个巨大的为此,有一百个条目的数组。当然,我想我可以这样做,但我觉得 Python 可能有一种非常简单的方法来做到这一点,而我却缺少这种方法。
谢谢各位!
所以我在 Python 3.7.4 中使用用户输入的日期,这些日期由另一个程序以字典格式存储在变量中。这些可能是最近的任何日期。例如,2019 年 11 月 6 日如下所示:
{'Select start date': datetime.datetime(2019, 11, 6, 0, 0)}
Run Code Online (Sandbox Code Playgroud)
我不关心项目标签,但我想将此字典日期转换为 2019 年 11 月 6 日的格式(我知道是strftime('%d %b %Y')),但我不确定如何让它理解上面是日期时间对象并在它实际上是字典对象时对其进行转换,并且不会抛出错误。
我已经在这里阅读了很多关于此的内容,但这里几乎所有的问题都只关注今天的日期(如此datetime.datetime.now()使用)或特定日期,而不是用户输入的可能会有所不同的日期,并且存在于字典中。我已经看到很多这样的东西:
import datetime
d = datetime.datetime(2019, 11, 06, 0, 0, 0, 000000)
d.strftime("%a %b %d %Y %H:%M:%S %z")
Run Code Online (Sandbox Code Playgroud)
...但它似乎并不完全适用于这种情况。由于字典格式,像strftime和strptime这样的命令似乎不起作用,而且我不能使用像上面这样的静态示例,因为我要转换的实际日期并不总是相同的。如何在不使用字符串操作的情况下进行一些疯狂的工作?我觉得我缺少一个非常简单的解决方案。
代码示例(不起作用):
import datetime
dic = {'Select start date': datetime.datetime(2019, 11, 7, 0, 0)}
for key, value in dic.items():
d = datetime.datetime(value)
d.strftime("%d %b %Y")
Run Code Online (Sandbox Code Playgroud)
产生错误:
TypeError: an …Run Code Online (Sandbox Code Playgroud) 我有一个在 Windows 中运行的非常大的 Python 3.x 程序。它在 99.9% 的情况下都运行良好,但偶尔会崩溃。我不确定是什么导致了崩溃,可能有很多原因。由于出于安全原因,我必须使用不可见的控制台运行“编译”的 .exe 程序(不要问),因此当它死机时,我看不到任何形式的控制台读数。所以显然,如果我可以让它将崩溃回溯输出为文本文件,那就太好了。
我熟悉 Python 中的 try/except,但导致问题的代码段可能在任何地方,我不想在数千行代码的每一行周围编写单独的 try/except。有没有办法让程序始终将任何程序停止错误输出为文本文件,无论哪一行代码导致问题,或者错误可能是什么?
在 Python 3.8 中,我试图让浮点值显示如下:
我知道“圆”。如果我有这个程序:
print ('input a number')
chuu = float(input())
chuu = round(chuu,2)
print (chuu)
Run Code Online (Sandbox Code Playgroud)
我也知道我可以这样做:
print ('input a number')
chuu = float(input())
print('{0:.2f}'.format(chuu))
Run Code Online (Sandbox Code Playgroud)
这也没有得到我想要的:
我该如何解决这个问题?