相关疑难解决方法(0)

'str'对象没有属性'标点符号'

我可能在这里真的很蠢,但我无法弄清楚这个错误:

'str' object has no attribute 'punctuation'
Run Code Online (Sandbox Code Playgroud)

这发生在这一行:

docLines[counter][counter2] = [(docLines[counter][counter2]).translate(None, string.punctuation)]
Run Code Online (Sandbox Code Playgroud)

哪里__CODE__只是一个单词.

我出错的任何想法?

python string

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

文本Python中重复的短语

我有一个问题,我不知道如何解决它.请提出一些建议.

我有一个文字.大,大文.任务是找到文本中长度为3(包含三个单词)的所有重复短语.

python text repeat

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

有效地删除标点而不是".com"

我找到了这个线程:从Python中删除字符串标点符号的最佳方法

但希望能想出办法来做到这一点,除非不删除链接中的句号.所以,如果字符串是

I love using stackoverflow.com on Fridays, Saturdays and Mondays!
Run Code Online (Sandbox Code Playgroud)

它会回来

I love using stackoverflow.com on Fridays Saturdays and Monday
Run Code Online (Sandbox Code Playgroud)

事实上,理想情况下,我可以传递一个常见的链接结尾列表,如.com,.net,.ly等.

python string

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

使用python从字符串中删除所有空格和引号的最有效方法是什么?

可能重复:
从Python中删除字符串标点符号的最佳方法

我有一个字符串,我想在输出上放两个双引号"{{ var }}",所以我想确保从用户提供的字符串的末尾删除所有单/双引号.实现这一目标的最有效方法是什么?

输入/期望输出:

   """""""""" string here '''''''''''''''         => 'string here'
       string'''''''""""""''''''"""""  => 'string'
Run Code Online (Sandbox Code Playgroud)

python regex string

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

Python - 从列表中删除标点符号

我需要从文本文件中删除punc.

文本文件是这样的

ffff,hhhh和tommorw回家,
你离开了吗?

我在尝试

PUNC =(",/;?& - ")

f = open('file.txt','r')

for line in f:
    strp=line.replace(punc,"")
    print(strp)
Run Code Online (Sandbox Code Playgroud)

我需要输出为:

ffff hhhh tommorw home

Have you from gone
Run Code Online (Sandbox Code Playgroud)

这是返回每一行,但punc仍然存在>可以使用一些帮助.谢谢

python python-3.x

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

如何从文本文件中删除标点符号

import collections
import string
with open('cipher.txt') as f:
  f = f.read().replace(' ', '').replace('\n','').lower()
  f = f.strip(string.punctuation)

cnt = collections.Counter(f.replace(' ', ''))
for letter in sorted(cnt):
  print(letter, cnt[letter])
Run Code Online (Sandbox Code Playgroud)

我该如何删除标点符号!! 我无法弄清楚该线的位置?有人可以修改我的代码以删除除字母之外的所有内容吗?谢谢

python python-3.x

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

从String中删除char,有什么更有效的方法呢?

我目前正在从在线课程学习Python 2.7.其中一个问题是我必须从字符列表中删除字符串中的字符.

我做的是:

def getAvailableLetters(letters):

    alphabet = string.ascii_lowercase
    reduced_alphabet = ''

    for char in alphabet:
        if char not in lettersGuessed:
            reduced_alphabet += char

    return reduced_alphabet
Run Code Online (Sandbox Code Playgroud)

我已经知道没有字符串方法可以直接从字符串中删除字符串,因为它们是不可变的,所以我想出了这个.我已经成功地提交了一个正确的答案,但我对此并不十分满意,因为我觉得有一种更有效的方法.

python string performance python-2.7

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

在Python中消除一系列.replace()调用

我正在开发一个涉及解析文本页面的项目.我编写了以下函数来从单词中删除某些标点符号并将其转换为小写:

def format_word(word):
    return word.replace('.', '').replace(',', '').replace('\"', '').lower()
Run Code Online (Sandbox Code Playgroud)

有没有办法将.replace()的所有调用合并为一个?这看起来很丑陋!我能想到的唯一方法是:

def format_word(word):
    for punct in '.,\"':
        word.replace(punct, '')
    return word.lower()
Run Code Online (Sandbox Code Playgroud)

python string syntax code-cleanup

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

如何使用string.punctuation自定义筛选出的字符?

我有一个字符串,我想删除所有标点符号.我目前使用:

import string
translator = str.maketrans('','', string.punctuation)
name = name.translate(translator)
Run Code Online (Sandbox Code Playgroud)

但是,对于名字的字符串,这也删除了连字符,我想保留在字符串中.对于实例'\ Fred-Daniels!"应该成为"Fred-Daniels".

如何修改上面的代码来实现这个目的?

python regex string

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

从Python字符串中删除标点符号

我似乎有一个问题从Python中的字符串中删除标点符号.在这里,我给了一个文本文件(特别是Project Gutenberg的一本书)和一个停用词列表.我想返回10个最常用单词的字典.不幸的是,我在返回的字典中不断打嗝.

import sys
import collections
from string import punctuation
import operator

#should return a string without punctuation
def strip_punc(s):
    return ''.join(c for c in s if c not in punctuation)

def word_cloud(infile, stopwordsfile):

    wordcount = {}

    #Reads the stopwords into a list
    stopwords = [x.strip() for x in open(stopwordsfile, 'r').readlines()]


    #reads data from the text file into a list
    lines = []
    with open(infile) as f:
        lines = f.readlines()
        lines = [line.split() for line in lines]

    #does the wordcount …
Run Code Online (Sandbox Code Playgroud)

python nlp

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

使用 string.punctuation python 时出错

我尝试在 python 中使用 string.punctuation 函数。我在 python 3.5 版本中这样做。这是我的以下代码:

import string
def check(text):
    x=set(text.punctuation())
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

'str' object has no attribute 'punctuation'
Run Code Online (Sandbox Code Playgroud)

请帮助正确使用 this 的方法。我什至尝试使用 ispunct() 功能。

我尝试解决这个问题,如下面的链接所示,但无法弄清楚。

在 Python 中从字符串中去除标点符号的最佳方法

python string punctuation python-3.x

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

坏字符范围:-'在python中的位置6异常

我正在比较两个字符串,但不包括两者中的标点符号。

这是我的代码片段:

punctuation = r"[.?!,;:-']"
string1 = re.sub(punctuation, r"", string1)
string2 = re.sub(punctuation, r"", string2)
Run Code Online (Sandbox Code Playgroud)

运行此代码后,我得到以下异常

punctuation = r"[.?!,;:-']"
string1 = re.sub(punctuation, r"", string1)
string2 = re.sub(punctuation, r"", string2)
Run Code Online (Sandbox Code Playgroud)

如何摆脱这个异常?“坏字符范围”是什么意思?

python regex

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

while循环中的String.replace永远不会停止

我有一个符号列表,我想用字符串中的空格替换.这是我的代码:

useless = ['.', ',', '!', '?', ':', '(', ')', '"', '[', ']']
message = 'Whatever. you; "want:)'

for sign in useless:
    while message.find(sign):
        message.replace(sign, ' ')
Run Code Online (Sandbox Code Playgroud)

这导致永不停止的无限循环.这可能很容易,但我现在已经很长时间了..任何帮助都会很棒,谢谢.

python

-1
推荐指数
1
解决办法
346
查看次数