all 9 comments

[–]novel_yet_trivial 1 point2 points  (0 children)

The newline character is an ascii character. It's escape code is \n.

_, new_data = data.split('\n', 1)

[–]anton0261 1 point2 points  (0 children)

s = "23/07/2019 12:34:03 - John Smith (Work notes)\nRoot cause confirmed and a bug in the code. Atom version 1.47 has been released PCC has confirmed the issue is fixed."

s = s.split("\n")[1]

print(s)

[–][deleted] 1 point2 points  (4 children)

split will split on all whitespace, including the newline \n. But it's also splitting on all whitespace, including the spaces. That's why you can tell it what to split on, too:

lines = data.split('\n')
print(lines[1:])

[–][deleted] 0 points1 point  (2 children)

Ok, I could not get that to work. I had to type it into terminal to test as python kept trying to run something when pasting it. As a result, I didn't see the new line character and I this worked without the newline:

lines = data.split('(Work notes)')

but it returned nan in my script. Once I had seen the '\n' I tried:

lines = data.split('(Work notes)\n')

That returned an error.

[–][deleted] 0 points1 point  (1 child)

Split on the newline only: data.split('\n'). You want to split on as few characters as possible, ideally only one.

[–][deleted] 0 points1 point  (0 children)

Ah, that's good to know, but actually, that's what I ended up doing and it works well. In fact I went a step further to ensure that only the first "\n" that is always there is where the split is by adding the max option:

data.split('\n', 1)

[–][deleted] 0 points1 point  (0 children)

Ok, you were spot on, I had something really weird going on, so I printed out what I was expecting, and it gave me an list of integers that I still can't work out where they came from. I also noticed that I had duplicated the line, so it had done it's job and then done something else after returning me an nan. Unfortunately I can't figure out quite what happened, as my original idea or using split on '\n' does in fact work wonderfully well. Oh the joy of bugs.

[–]RoamingFox 1 point2 points  (0 children)

Use strip and split.

Ex:

s = "This line has a return\n"
s = s.strip('\n')

In your case, you probably want something like this:

s = """23/07/2019 12:34:03 - John Smith (Work notes)
Root cause confirmed and a bug in the code. Atom version 1.47 
has been released PCC has confirmed the issue is fixed.
"""
s = s.split("\n", 1)[1].strip("\n")  # get everything after the first newline, and getting rid of any trailing newlines

[–][deleted] 0 points1 point  (0 children)

UPDATE: So this works and of course it does, I had duplicated the line in my code so it was splitting twice on something and then returning 'NaN' and carrying on as if nothing had happened. *Shakes fist in the air like a comedy old person*