我正在使用Requests-Cache库来缓存Requests的结果。看来安装缓存就好了;请求 URL 会创建一个.sqlite缓存文件,后续请求会检索该数据,即使远程页面发生更改也是如此。
今天我的互联网连接很差,我注意到我的脚本(它发出许多(据说是缓存的)请求)运行缓慢。作为快速健全性检查,我尝试使用测试脚本来创建缓存,然后在断开计算机与 WiFi 的连接后再次运行它。但是,这会出错:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='magicplugin.normalitycomics.com', port=80): Max retries exceeded with url: /update/updatelist.txt (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x110390d68>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))
Run Code Online (Sandbox Code Playgroud)
如果 Requests-Cache 将请求重定向为使用本地缓存数据,为什么请求甚至尝试连接到远程站点?有办法避免这种情况吗?我不需要减慢我的脚本速度(特别是当我的连接很差时)并从服务器发出不必要的请求。
示例数据来说明:
\nimport pandas as pd\n\nanimals = pd.DataFrame({\'name\': [\'ostrich\', \'parrot\', \'platypus\'],\n \'legs\': [2, 2, 4],\n \'flight\': [False, True, False],\n \'beak\': [True, True, True],\n \'feathers\': [True, True, False]})\nRun Code Online (Sandbox Code Playgroud)\n| 姓名 | 腿 | 航班 | 喙 | 羽毛 |
|---|---|---|---|---|
| 鸵鸟 | 2 | \xe2\x9c\x94 | \xe2\x9c\x94 | |
| 鹦鹉 | 2 | \xe2\x9c\x94 | \xe2\x9c\x94 | \xe2\x9c\x94 |
| 鸭嘴兽 | 4 | \xe2\x9c\x94 |
Pandas 可以轻松地根据条件检查整个列(这是一个系列),并且结果(一系列布尔值)可用于通过布尔索引过滤数据帧:
\nbipeds = (animals.legs == 2)\nprint(animals[bipeds])\n\n name legs flight beak feathers\n0 ostrich 2 False True True\n1 parrot 2 True True True\nRun Code Online (Sandbox Code Playgroud)\n在我的用例中,每个这样的条件都是从文本搜索字符串中的术语解析的,因此我需要以编程方式构造它们。(我知道 Pandas 的查询 …
我找不到一种可以在 python 中使用的方式读取 Minecraft 世界文件的方法
我浏览了互联网,但找不到任何教程,只有几个库声称他们可以做到这一点,但从未真正起作用
from nbt import *
nbtfile = nbt.NBTFile("r.0.0.mca",'rb')
Run Code Online (Sandbox Code Playgroud)
我预计这会起作用,但我收到了有关文件未压缩或类似内容的错误
完整错误:
Traceback (most recent call last):
File "C:\Users\rober\Desktop\MinePy\MinecraftWorldReader.py", line 2, in <module>
nbtfile = nbt.NBTFile("r.0.0.mca",'rb')
File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py", line 628, in __init__
self.parse_file()
File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py", line 652, in parse_file
type = TAG_Byte(buffer=self.file)
File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py", line 99, in __init__
self._parse_buffer(buffer)
File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nbt\nbt.py", line 105, in _parse_buffer
self.value = self.fmt.unpack(buffer.read(self.fmt.size))[0]
File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\gzip.py", line 276, in read
return self._buffer.read(size)
File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\_compression.py", line 68, in readinto
data = self.read(len(byte_view))
File "C:\Users\rober\AppData\Local\Programs\Python\Python36-32\lib\gzip.py", …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Pillow (python-imaging-library) Python 库在我的 .png 图像周围创建轮廓/描边/边框(选择任何颜色和宽度)。您可以在这里看到原始图像和我想要的结果(由手机应用程序创建):

您可以在这里下载原始图像的png文件:https://pixabay.com/illustrations/brain-character-organ-smart-eyes-1773885/
我已经用中等尺寸(1280x1138)完成了它,但也许用最小尺寸(640x569)来完成它更好。
我尝试用两种方法解决这个问题。
方法一
第一种方法是创建 Brain.png 图像的全黑图像,将其放大,然后将原始彩色大脑图像粘贴在其上。这是我的代码:
brain_black = Image.open("brain.png") #load brain image
width = brain_black.width #in order not to type a lot
height = brain_black.height #in order not to type a lot
rectangle = Image.new("RGBA", (width, height), "black") #creating a black rectangle in the size of the brain image
brain_black.paste(rectangle, mask=brain_black) #pasting on the brain image the black rectangle, and masking it with the brain picture
#now brain_black is the brain.png …Run Code Online (Sandbox Code Playgroud) 我无法使用PyFPDF输出 HTML 消息。当我尝试将HTML 写入 PDF 文档时,出现错误:
Traceback (most recent call last):
File "/Users/elcid/Projects/so_test/test.py", line 8, in <module>
pdf.write_html("<b>Sample HTML</b>")
File "/Users/elcid/.pyenv/versions/so/lib/python3.9/site-packages/fpdf/html.py", line 400, in write_html
text = h2p.unescape(text) # To deal with HTML entities
AttributeError: 'HTML2FPDF' object has no attribute 'unescape'
Run Code Online (Sandbox Code Playgroud)
违规代码:
from fpdf import FPDF, HTMLMixin
class PDF(FPDF, HTMLMixin):
pass
pdf = PDF()
pdf.add_page()
pdf.write_html("<b>Sample HTML</b>")
pdf.output("html.pdf")
Run Code Online (Sandbox Code Playgroud)
我正在使用Python 3.9。有什么见解吗?谢谢你!
我有以下函数,它接受形状为 (20,000 x 20,000) 的指示矩阵。我必须运行该函数 20,000 x 20,000 = 400,000,000 次。请注意,indicator_Matrix当作为参数传递给函数时,必须采用 pandas 数据帧的形式,因为我实际问题的数据帧具有 timeIndex 和整数列,但为了理解问题,我对此进行了一些简化。
indicator_Matrix = pd.DataFrame(np.random.randint(0,2,[20000,20000]))
def operations(indicator_Matrix):
s = indicator_Matrix.sum(axis=1)
d = indicator_Matrix.div(s,axis=0)
res = d[d>0].mean(axis=0)
return res.iloc[-1]
Run Code Online (Sandbox Code Playgroud)
我尝试通过使用来改进它numpy,但它仍然需要很长时间才能运行。我也尝试过concurrent.future.ThreadPoolExecutor,但仍然需要很长时间才能运行,并且列表理解没有太大改进。
indicator_Matrix = pd.DataFrame(np.random.randint(0,2,[20000,20000]))
def operations(indicator_Matrix):
s = indicator_Matrix.to_numpy().sum(axis=1)
d = (indicator_Matrix.to_numpy().T / s).T
d = pd.DataFrame(d, index = indicator_Matrix.index, columns = indicator_Matrix.columns)
res = d[d>0].mean(axis=0)
return res.iloc[-1]
output = [operations(indicator_Matrix) for i in range(0,20000**2)]
Run Code Online (Sandbox Code Playgroud)
请注意,我再次转换为数据框的原因d是因为我需要获取列均值并仅保留最后一列均值,使用.iloc[-1]. …
该网站上有许多帖子通常顺便提及pip_interop_enabled=True在某些环境中进行设置的想法。据我所知,这使得 conda 和 pip3 在某种程度上可以更好地交互。准确地说,如果这是真的,人们说 conda 将在 PyPI 中搜索主通道中不存在的包。他们还说这是“实验性的”。
这是conda关于此的文档。它指出,即使使用 pip_interop_enabled=False,最近版本中 conda 的大部分行为也得到了改进,导致人们质疑此设置的作用。
我的问题是:实际上,这一切意味着什么?
我检查了Python 3中的运算符优先级,发现没有赋值(=)。
我想知道赋值是否是运算符。如果不是,为什么 Google 搜索时会出现这么多“赋值运算符”?与其他实数运算符(布尔运算符、比较运算符等)的优先级关系如何?
如何删除路径并仅将文件名和扩展名保留在变量中?
root=tk.Tk()
root.withdraw()
FileName=filedialog.askopenfilenames()
print(Filename)
Run Code Online (Sandbox Code Playgroud)
我只想要例如namefile.txt而不是整个路径,例如/path/to/namefile.txt.
from decimal import *
Pi=Decimal(3.141592653589793238462643383279502884197169399373)
print(Pi)
Run Code Online (Sandbox Code Playgroud)
实际输出:
3.141592653589793115997963468544185161590576171875
Run Code Online (Sandbox Code Playgroud)
输出应该是:
3.141592653589793238462643383279502884197169399373
Run Code Online (Sandbox Code Playgroud)
为什么值会改变?