小编Cha*_*han的帖子

无法从正在运行的事件循环中调用asyncio.run()

我想使用asyncio获取网页html。

我在jupyter笔记本中运行以下代码:

import aiofiles
import aiohttp
from aiohttp import ClientSession

async def get_info(url, session):
    resp = await session.request(method="GET", url=url)
    resp.raise_for_status()
    html = await resp.text(encoding='GB18030')
    with open('test_asyncio.html', 'w', encoding='utf-8-sig') as f:
        f.write(html)
    return html

async def main(urls):
    async with ClientSession() as session:
        tasks = [get_info(url, session) for url in urls]
        return await asyncio.gather(*tasks)

if __name__ == "__main__":
    url = ['http://huanyuntianxiazh.fang.com/house/1010123799/housedetail.htm', 'http://zhaoshangyonghefu010.fang.com/house/1010126863/housedetail.htm']
    result = asyncio.run(main(url))
Run Code Online (Sandbox Code Playgroud)

但是,它返回 RuntimeError: asyncio.run() cannot be called from a running event loop

问题是什么?

怎么解决呢?

非常感谢你。

python-3.x python-asyncio jupyter-notebook

7
推荐指数
6
解决办法
5784
查看次数

如何使用opencv(python)从url读取gif

我可以使用cv2读取jpg文件

import cv2
import numpy as np
import urllib
url = r'http://www.mywebsite.com/abc.jpg'
req = urllib.request.urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1)
cv2.imshow('abc',img)
Run Code Online (Sandbox Code Playgroud)

但是,当我使用gif文件时,它会返回一个错误:

error: (-215) size.width>0 && size.height>0 in function cv::imshow
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

python opencv

6
推荐指数
1
解决办法
7033
查看次数

熊猫ValueError:模式不包含捕获组

使用正则表达式时,我得到:

import re
string = r'http://www.example.com/abc.html'
result = re.search('^.*com', string).group()
Run Code Online (Sandbox Code Playgroud)

在大熊猫中,我写道:

df = pd.DataFrame(columns = ['index', 'url'])
df.loc[len(df), :] = [1, 'http://www.example.com/abc.html']
df.loc[len(df), :] = [2, 'http://www.hello.com/def.html']
df.str.extract('^.*com')

ValueError: pattern contains no capture groups
Run Code Online (Sandbox Code Playgroud)

如何解决问题?

谢谢。

python pandas

6
推荐指数
3
解决办法
4076
查看次数

How to save pandas to excel with different colors

I have a dataframe

df = pd.DataFrame(columns = ['id', 'name', 'age'])
df.loc[len(df), :] = [1, 'John', 21]
df.loc[len(df), :] = [2, 'Mary', 19]
df.loc[len(df), :] = [3, 'Ann', 27]
df.loc[len(df), :] = [4, 'Ben', 18]
Run Code Online (Sandbox Code Playgroud)

I want to save it to excel file using xlsxwriter.

However, I want the age larger than 20 to be in red.

In other words, 21 and 27 should appear red in the excel file.

How to do it?

python dataframe

5
推荐指数
1
解决办法
7367
查看次数

如何根据行值获取pandas中的列名?

我有一个数据帧,df:

        id_0         id_1          id_2
0         1            0             1
1         1            0             0
2         0            1             0
3         1            1             0
4         0            0             1
5         0            0             0
Run Code Online (Sandbox Code Playgroud)

我想获得每行的列名称,如果有1.如何做到这一点?谢谢.

结果:

           result
0         id_0, id_2
1         id_0
2         id_1
3         id_0, id_1
4         id_2
5         NaN
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

4
推荐指数
1
解决办法
109
查看次数

如何在谷歌存储桶中读取、写入和列出文件夹和文件?

我想用 Python 读/写 Google Cloud Storage 存储桶中的文件。

假设我在gs://my_project/data.

  • 如何列出上述文件夹中的文件夹和文件?

  • 如何读写文件?

python google-cloud-storage

4
推荐指数
1
解决办法
9573
查看次数

在 Python 中使用正则表达式提取子字符串

如何在关键字之后提取子字符串amisare从字符串中提取但不包含amisare

string = 'I am John'
Run Code Online (Sandbox Code Playgroud)

我用了:

re.findall('(?<=(am|is|are)).*', string)
Run Code Online (Sandbox Code Playgroud)

发生错误

re.error: look-behind requires fixed-width pattern
Run Code Online (Sandbox Code Playgroud)

正确的做法是什么?

python regex

3
推荐指数
1
解决办法
3811
查看次数

TensorFlow FailedPreconditionError:迭代器尚未初始化

我想显示张量的值。

这是我的代码:

#some code here
data = [data_tensor for data_tensor in data_dict.items()]
for i in data:
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print (sess.run(i[1]))
        print('_'*100)
Run Code Online (Sandbox Code Playgroud)

但是,我得到了错误:

FailedPreconditionError (see above for traceback): 
GetNext() failed because the iterator has not been initialized. 
Ensure that you have run the initializer operation for this iterator 
before getting the next element.
Run Code Online (Sandbox Code Playgroud)

如何解决问题?

非常感谢你。

python tensorflow

2
推荐指数
1
解决办法
1457
查看次数

使用Selenium启动网站时如何禁用自动播放

某些网站会自动播放视频或放大图像。如何使用Selenium和Python停止它?

非常感谢你。

javascript python selenium

0
推荐指数
1
解决办法
1500
查看次数