我有一个带有以下内容的文件:
require('../services/customError')
我尝试剥离这些行并拆分它们,然后尝试剥离每个列表中带有标点符号的子字符串。
<script>
var mapping = {{ data | tojson }};
var val = "Bird";
var something = mapping[val];
</script>
我应该怎么做才能将这些行变成两个列表,就像这样:
"[36.147315849999998, -86.7978174] 6 2011-08-28 19:45:11 @maryreynolds85 That is my life, lol."
"[37.715399429999998, -89.21166221] 6 2011-08-28 19:45:41 Ate more veggie and fruit than meat for the first time in my life"
答案 0 :(得分:2)
所有数据都采用相同格式吗?如果是,请使用re
库中的正则表达式。
import re
your_str="[36.147315849999998, -86.7978174] 6 2011-08-28 19:45:11 @maryreynolds85 That is my life, lol."
reg_data= re.compile(r"\[(.*),(.*)\] (.*)")
your_reg_grp=re.match(reg_data,your_str)
if your_reg_grp:
print(your_reg_grp.groups())
#这应该将所有内容都放在列表中,除了方括号之外的部分,您可以通过split(“”)拆分最后一个,然后创建一个新列表。
grp1=your_reg_grp.groups()
grp2=grp1[-1].split(" ")
组合grp1 [:-1]和grp2
答案 1 :(得分:-1)
您已经在列表中创建需要的单词。您只需创建一个列表并将其添加到列表中即可。
with open('aabb.txt') as t:
for Line in t:
list=[]
splitline = Line.strip()
splitline2 = splitline.split()
for words in splitline2:
words = words.strip("!#$%&'()*+,-./:;?@[\]^_`{|}~")
words = words.lower()
list.append(words)
print(list)
您还可以为每行创建一个列表列表,并将其用于您的需求。
with open('aabb.txt') as t:
root_list=[]
for Line in t:
temp_list=[]
splitline = Line.strip()
splitline2 = splitline.split()
for words in splitline2:
words = words.strip("!#$%&'()*+,-./:;?@[\]^_`{|}~")
words = words.lower()
temp_list.append(words)
root_list.append(temp_list)
print(root_list)