【问题标题】:Python line.split only splits single charactersPython line.split 仅拆分单个字符
【发布时间】:2012-05-23 16:19:41
【问题描述】:

我有一个这样的字符串数组:

一些标题##DD-MM-JJJJ##一些文字在这里##img1.jpg##img2.jpg 我想在## 处拆分这个字符串。我的代码如下:

with open("raw_news.txt", "r") as f:
    raw = []
    for line in f:
            line.strip()
            line.split('##')
            raw.append(line)

它不起作用。我只收到单个字母。 re.split 也没有成功。我真的很茫然,有人知道我做错了什么吗?

【问题讨论】:

    标签: python split delimiter


    【解决方案1】:

    问题是你忽略了split()的返回值:

                raw.append(line.split('##'))
    

    例如:

    In [5]: s = "Some Title##DD-MM-JJJJ##Some Text goes here##img1.jpg##img2.jpg"
    
    In [6]: s.split("##")
    Out[6]: ['Some Title', 'DD-MM-JJJJ', 'Some Text goes here', 'img1.jpg', 'img2.jpg']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2016-04-08
      相关资源
      最近更新 更多