我的笔记本中正在丢弃一些print()输出,但我看不到任何特定的模式或原因:

s = 'https://ad.doubleclick.net/'
t = 'ttps://ad.doubleclick.net/'
u = 'https://ad.doubleclick.net'
v = 'https://ad.doublclick.net/'
w = 'https://pubads.g.doubleclick.net/gampad/ads?caps'

for string in [s, t, u, v]:
    print('str:', string)
    print('repr(str):', repr(string))
    print()

输出:

str: 
repr(str): 'https://ad.doubleclick.net/'

str: ttps://ad.doubleclick.net/
repr(str): 'ttps://ad.doubleclick.net/'

str: https://ad.doubleclick.net
repr(str): 'https://ad.doubleclick.net'

str: https://ad.doublclick.net/
repr(str): 'https://ad.doublclick.net/'

str: 
repr(str): 'https://pubads.g.doubleclick.net/gampad/ads?caps'

我的第一行和最后str:行怎么了? 只发生在笔记本电脑上,在终端运行Python解释器不会有这个问题.

Python3.8.5

> which jupyter
/Library/Frameworks/Python.framework/Versions/3.8/bin/jupyter

> jupyter --version
Selected Jupyter core packages...
IPython          : 8.0.0
ipykernel        : 6.7.0
ipywidgets       : 7.7.1
jupyter_client   : 7.1.1
jupyter_core     : 4.9.1
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : 0.5.10
nbconvert        : 6.4.0
nbformat         : 5.1.3
notebook         : 6.4.7
qtconsole        : not installed
traitlets        : 5.1.1

推荐答案

这不是一个确切的答案,但这是Jupyter笔记本判断字符串变量是否为URL的结果,如果是,则使它们成为输出区域中可点击的链接.单元格的输出在<pre>标记内呈现,如下所示:

<pre>
str: <a target="_blank" href="https://ad.doubleclick.net/">https://ad.doubleclick.net/</a>
repr(str): 'https://ad.doubleclick.net/'

str: ttps://ad.doubleclick.net/
repr(str): 'ttps://ad.doubleclick.net/'

str: <a target="_blank" href="https://ad.doubleclick.net">https://ad.doubleclick.net</a>
repr(str): 'https://ad.doubleclick.net'

str: <a target="_blank" href="https://ad.doublclick.net/">https://ad.doublclick.net/</a>
repr(str): 'https://ad.doublclick.net/'

str: <a target="_blank" href="https://pubads.g.doubleclick.net/gampad/ads?caps">https://pubads.g.doubleclick.net/gampad/ads?caps</a>
repr(str): 'https://pubads.g.doubleclick.net/gampad/ads?caps'
</pre>

现在,一些链接没有出现的事实不是Jupyter笔记本的问题,你可以try 在https://html.onlineviewer.net这样的网站上呈现这个HTML,并看到同样的问题.但对我来说,这个问题只在Firefox中出现,而不是在我判断过的另外两个浏览器(Safari和Chrome)中.由于我找不到任何关于这种行为的讨论,我在Bugzilla here上开了一张罚单.

Python-3.x相关问答推荐

我的SELECT函数搜索的是列,而不是列中的数据.我怎么才能让它搜索数据呢?

丢弃重复的索引,并在多索引数据帧中保留一个

将数据帧扩展为矩阵索引

如何在M x N数组的行中找到所有值的组合

PyTest:尽管明确运行了测试,但是被标记为没有运行测试

为什么不能用格式字符串 '-' 绘制点?

根据另一个数据帧中的位置从主数据帧中提取子序列

在字符串中查找正则表达式的所有模式

正则表达式:匹配字符串中的分隔符(字母和特殊字符)以形成新的子字符串

无法使用 Python 和 Selenium 检索 href 属性

参数化泛型不能与类或实例判断一起使用

如何从左到右解包元组?

使用 Python 3 按行进行分析

multiprocessing.Queue 中的 ctx 参数

__new__ 方法给出错误 object.__new__() 只接受一个参数(要实例化的类型)

Python3 mysqlclient-1.3.6(又名 PyMySQL)的用法?

TypeError: write() 参数必须是 str,而不是字节(Python 3 vs Python 2)

在 linux mint 上安装 python3-venv 模块

哪个更有效:Python 文档字符串还是类型提示?

在 PostgreSQL 上使用 SQLAlchemy 创建全文搜索索引