相关疑难解决方法(0)

如何将字符串拆分为列表?

我希望我的Python函数分割一个句子(输入)并将每个单词存储在一个列表中.我当前的代码拆分了句子,但没有将单词存储为列表.我怎么做?

def split_line(text):

    # split the text
    words = text.split()

    # for each word in the line:
    for word in words:

        # print the word
        print(words)
Run Code Online (Sandbox Code Playgroud)

python split list text-segmentation

545
推荐指数
8
解决办法
170万
查看次数

在Python中的空白上拆分字符串

我正在寻找Python的等价物

String str = "many   fancy word \nhello    \thi";
String whiteSpaceRegex = "\\s";
String[] words = str.split(whiteSpaceRegex);

["many", "fancy", "word", "hello", "hi"]
Run Code Online (Sandbox Code Playgroud)

python regex string whitespace split

416
推荐指数
4
解决办法
63万
查看次数

python拆分一个至少有2个空格的字符串

我想在只有至少两个或更多空格的地方拆分一个字符串.

例如

str = '10DEUTSCH        GGS Neue Heide 25-27     Wahn-Heide   -1      -1'
print str.split()
Run Code Online (Sandbox Code Playgroud)

结果:

['10DEUTSCH', 'GGS', 'Neue', 'Heide', '25-27', 'Wahn-Heide', '-1', '-1']
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样:

['10DEUTSCH', 'GGS Neue Heide 25-27', 'Wahn-Heide', '-1', '-1']
Run Code Online (Sandbox Code Playgroud)

python split python-2.7

29
推荐指数
3
解决办法
2万
查看次数

python在空格上拆分字符串

我正在尝试将复制/粘贴的文本转换为csv,我可以将其拆分.问题是它里面有空白标签,我似乎无法摆脱它

复制/粘贴示例:

Amarr Hybrid Tech Decryptor 12  Decryptors - Hybrid         12 m3
Ancient Coordinates Database    23  Sleeper Components          2.30 m3
Caldari Hybrid Tech Decryptor   17  Decryptors - Hybrid         17 m3
Carbon  17  General         34 m3
Cartesian Temporal Coordinator  4   Ancient Salvage         0.04 m3
Central System Controller   2   Ancient Salvage         0.02 m3
Run Code Online (Sandbox Code Playgroud)

现在我想尝试这样的事情:

Amarr Hybrid Tech Decryptor,12,Decryptors - Hybrid,12,m3,
Ancient Coordinates Database,23,Sleeper Components,2.30,m3,
Caldari Hybrid Tech Decryptor,17,Decryptors - Hybrid,17,m3,
Carbon,17,General,34,m3,
Cartesian Temporal Coordinator,4,Ancient Salvage,0.04,m3,
Central System Controller,2,Ancient Salvage,0.02,m3,
Run Code Online (Sandbox Code Playgroud)

(将始终是每行5个分色

我一直在尝试以各种方式执行此操作, 使用逗号分割并在Python中删除空格, …

python string whitespace split

6
推荐指数
1
解决办法
1万
查看次数