如何提取python字符串中的单词

问题描述 投票:-3回答:2

我有很多看起来像下面的字符串:

Einwohnerzahl

244.951

StudierendeamHochschulort
[...]
´´´

The words are separated now by "\n". 
How can I seperated the whole words and numbers in separate lists? Because I got a lot of these strings which are not exactly the same I'am looking for a solution which can handele variations like different positions of the words / numbers or missing words / numbers in the string.
python string extract word
2个回答
0
投票

使用此:

s = 'apple\nbanana' 
print(s.split('\n'))

输出:

['apple', 'banana']

如果您希望将其作为单独的词/短语:

s = 'apple\nbanana' 
for i in s.split('\n'):
    print(i)

输出:

apple
banana

-3
投票

https://www.geeksforgeeks.org/python-extract-words-from-given-string/该站点将帮助您解决问题。在发布堆栈溢出帖子之前,您应该尝试找到问题的答案。

© www.soinside.com 2019 - 2024. All rights reserved.