【发布时间】:2021-04-12 15:16:56
【问题描述】:
我有一个文本文件:
示例.txt
Hi I am student
I am from
我试过的是
import string
import re
def read_to_list1(filepath):
text_as_string = open(filepath, 'r').read()
x = re.sub('['+string.punctuation+']', '', text_as_string).split("\n")
for i in x:
x_as_string = re.sub('['+string.punctuation+']', '', i).split()
print(x_as_string)
read_to_list1('sample.txt')
这个结果
['Hi,'I','am','student']
['I','am','from']
我想要的结果是:
[['Hi,'I','am','student'],['I','am','from']]
【问题讨论】:
-
所有
string.punctuation处理是怎么回事?你能举一个例子来说明你正在尝试处理这些情况吗?
标签: python python-3.x string list