Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python line.split only splits single characters

I have an array of strings like this:

Some Title##DD-MM-JJJJ##Some Text goes here##img1.jpg##img2.jpg I'd like to split this string at ##. My code is the following:

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

It doesn't work. I only get the single letters. re.split didn't do the trick, either. I'm really at a loss here, anyone knows what I'm doing wrong?

like image 414
Cielocchi Avatar asked Dec 21 '25 23:12

Cielocchi


1 Answers

The problem is that you're disregarding the return value of split():

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

For example:

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']
like image 127
NPE Avatar answered Dec 24 '25 13:12

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!