Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文本文件中删除标点符号和数字时出错_Python_Parsing - Fatal编程技术网

Python 从文本文件中删除标点符号和数字时出错

Python 从文本文件中删除标点符号和数字时出错,python,parsing,Python,Parsing,我试图通过删除标点符号、数字和等来清理文本文件 我写这段代码的初衷是尝试删除标点符号: import string with open("uniquewords_list.txt") as f: L = sorted(word.strip(",") for line in f for word in line.split()) out = L.translate(string.maketrans(&

我试图通过删除标点符号、数字和
等来清理文本文件

我写这段代码的初衷是尝试删除标点符号:

import string
with open("uniquewords_list.txt") as f:

         L = sorted(word.strip(",") for line in f for word in line.split())
         
         out = L.translate(string.maketrans("",""), string.punctuation)

         with open('testing.txt', 'w') as filehandle:
              for listitem in out:
                  filehandle.write('%s\n' % listitem)
但是,我得到一个错误:

out = L.translate(string.maketrans("",""), string.punctuation)
AttributeError: 'list' object has no attribute 'translate'
我查阅了错误描述,但仍然无法修复它。建议


此外,要删除数字和字符,如
什么是有效的方法?

如错误消息所述,您不能对
列表
对象调用
翻译
方法。但是,作为列表成员的
str
对象具有此方法

下面是一个简单的惯用列表理解,它迭代列表的每个成员,并分别调用其
translate
方法:

out=[x.translate(string.maketrans(“,”),string.标点符号)表示L中的x]
如果您是一名初学者,那么这段等效的手写代码可能更具可读性:

out=[]
对于L中的x:
out.append(x.translate(string.maketrans(“,”),string.标点符号))

当然,只调用
maketrans
一次效率更高。

错误很明显。我认为这里的这行代码
L=sorted(f行中的word.strip(“,”)表示line in line.split())
返回一个列表类型,您试图使用
translate
属性作为它的说明,但它没有。我想您只需要检查一下在这个
L=sorted(word.strip(“,”)for line in f for word in line.split())
code上您真正想要的输出类型。您真正希望它使用
.translate
,是什么输出或输出类型?您希望留下什么?只有字母表和空格?是的,只有字母表
string.maketrans()
python2的特性,你真的在使用python2吗?现代项目通常应该针对当前推荐和支持的语言版本,即Python3。不,我正在使用Python3.6,可能还想看看。我的只是一个简单的解决方案,不是最有效的。谢谢你的回答。是的,我是一个初学者,看到不同的做事方式是令人兴奋的。
import string
with open("uniquewords_list.txt") as f:

     contents = f.read()
     remove_pool = string.punctuation + '0123456789'  # + etc
     contents = ''.join(ch for ch in contents if ch not in remove_pool)

     with open('testing.txt', 'w') as filehandle:
          filehandle.write(contents + '\n')