python字符串strip不适用于尾部的双qu

2024-06-16 13:54:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用python脚本自动完成一项任务,但遇到了这种奇怪的现象。我在SO中查找了相同的,但略有不同,所以我在这里使用简化的示例进行询问。
我在下面有一个名为test1.txt的文件。你知道吗

"https://papers.nips.cc/paper/7286-efficient-algorithms-for-non-convex-isotonic-regression-through-submodular-optimization" ## Efficient Algorithms for Non-convex Isotonic Regression through Submodular Optimization
"https://papers.nips.cc/paper/7287-structure-aware-convolutional-neural-networks" ## Structure-Aware Convolutional Neural Networks
"https://papers.nips.cc/paper/7288-kalman-normalization-normalizing-internal-representations-across-network-layers" ## Kalman Normalization: Normalizing Internal Representations Across Network Layers
"https://papers.nips.cc/paper/7289-hogwild-gibbs-can-be-panaccurate" ## HOGWILD!-Gibbs can be PanAccurate

还有python脚本任务.py你知道吗

import re

with open('test1.txt') as f:
    for line in f:
        #print line
        link = re.sub(" ##.*","",line)
        print link
        link1 = link.strip('\"')
        print link1

当我通过python quest.py执行它时,我得到

"https://papers.nips.cc/paper/7286-efficient-algorithms-for-non-convex-isotonic-regression-through-submodular-optimization"

https://papers.nips.cc/paper/7286-efficient-algorithms-for-non-convex-isotonic-regression-through-submodular-optimization"

"https://papers.nips.cc/paper/7287-structure-aware-convolutional-neural-networks"

https://papers.nips.cc/paper/7287-structure-aware-convolutional-neural-networks"

"https://papers.nips.cc/paper/7288-kalman-normalization-normalizing-internal-representations-across-network-layers"

https://papers.nips.cc/paper/7288-kalman-normalization-normalizing-internal-representations-across-network-layers"

"https://papers.nips.cc/paper/7289-hogwild-gibbs-can-be-panaccurate"

https://papers.nips.cc/paper/7289-hogwild-gibbs-can-be-panaccurate"

我想打印链接第一次与周围的双引号(=link),然后没有双引号(=link1)。但是为什么我看到link1后面的双引号呢?你知道吗


Tags: httpsforlinkbecanpaperccnon
2条回答

Python的^{}将删除前导和尾随的chars,但一旦到达不在chars中的字符,它就会停止。你知道吗

看起来您的link以一个换行字符结束,甚至在到达双引号之前剥离就停止了。(提示:print只添加一个换行符,在输出中有两个。)

要去掉双引号和换行符:

link1 = link.strip('"\n')

另外,值得一提的是(正如注释中的@glibdud所指出的那样),链接以换行符结尾的原因是文件迭代器不带换行符,sub表达式也不带换行符(因为.不包含换行符;要包含它,请添加^{}regex标志)。你知道吗

如果不想用引号打印,只需去掉双引号和换行符,如果想用双引号打印,只需去掉换行符

import re

with open('file.txt') as f:
    for line in f:
        if line.strip():
            #print line
            link = re.sub(" ##.*", "", line)
            #Print with double quotes
            print link.strip('\n')
            #Print without double quotes by replacing double quotes with empty char
            print link.strip('"\n')

            #Print without double quotes by removing double quotes entirely
            #print link.strip("\"")

然后输出

"https://papers.nips.cc/paper/7286-efficient-algorithms-for-non-convex-isotonic-regression-through-submodular-optimization"
https://papers.nips.cc/paper/7286-efficient-algorithms-for-non-convex-isotonic-regression-through-submodular-optimization
"https://papers.nips.cc/paper/7287-structure-aware-convolutional-neural-networks"
https://papers.nips.cc/paper/7287-structure-aware-convolutional-neural-networks
"https://papers.nips.cc/paper/7288-kalman-normalization-normalizing-internal-representations-across-network-layers"
https://papers.nips.cc/paper/7288-kalman-normalization-normalizing-internal-representations-across-network-layers
"https://papers.nips.cc/paper/7289-hogwild-gibbs-can-be-panaccurate"
https://papers.nips.cc/paper/7289-hogwild-gibbs-can-be-panaccurate

相关问题 更多 >