我的代码有以下输出
代码:text = soup.get_text()
输出:
Article Title
Some text: Text blurb.
More blurb.
Even more blurb.
Some more blurb.
Second Article Title
Some text: Text blurb.
More blurb.
Even more blurb.
Some more blurb.
接下来,当我执行test = text.splitlines()
时,输出更改为
u'Article Title', u'', u'Some text',u'Text blurb',u'More blurb',u'Even more blurb',u'Some more blurb',, u'', u'', u'', u'', u'',u'Second Article Title', u'', u'Some text:',u'Text blurb',u'More blurb',u'Even more blurb',u'Some more blurb',, u'', u'', u'', u'', u'',
我想使用u'', u'', u'', u'', u''
分割字符串,以便随后分别解析各行。我本来希望使用这些标签,但是它们的结构使其难以使用。
如何执行拆分?我尝试过:
result = [list(g) for k,g in groupby(test,lambda x:x=="u''") if not k]
print result
和
for item in test:
arr = re.split("u'', u'', u'', u'', u''",item, flags=re.UNICODE)
print arr
但是他们没有给我想要的输出。
答案 0 :(得分:0)
如果您查看文字,则希望通过重复的换行符samp1 = [1, 3, 5, 7, 9, 11, 13]
samp2 = [2, 4, 6, 8, 10, 12, 14]
def prac(ncoll, samp1, samp2):
m = 4*pc.u
sp1 = []
sp2 = []
for i in range(ncoll):
a = random.choices(samp1)
b = random.choices(samp2)
sp1.append(a)
sp2.append(b)
np.concatenate(sp1)
np.concatenate(sp2)
en1 = 0.5*m*np.square(sp1)
en2 = 0.5*m*np.square(sp2)
diff = (en1-en2)
absdiff = np.abs(diff)
halfen = 0.5*absdiff
if diff[i] >= 0:
finalen1 = en1[i] - halfen
finalen2 = en2[i] + halfen
else:
finalen1 = en1[i] + halfen
finalen2 = en2[i] - halfen
finalsp1 = np.sqrt((2*finalen1)/m)
finalsp2 = np.sqrt((2*finalen2)/m)
return finalsp1, finalsp2
print(prac(2, samp1, samp2))
来分隔
\n
然后,您可以仅使用text
>> 'Article Title\n\n Some text: Text blurb.\n\nMore blurb.\n\nEven more blurb. \n\nSome more blurb. \n\n\n\n\n\nSecond Article Title\n\nSome text: Text blurb.\n\nMore blurb.\n\nEven more blurb. \n\nSome more blurb. '
的定义参数,如果不添加参数,Python只会按空格分隔。第一次拆分后,您可以按text.split('\n\n\n\n\n')
拆分其他元素。
\n\n