0

I have the following data in a csv file:

 IDA/IDB/type/timestamp valueoftype

 A1/B1/a/1575033906 4
 A1/B1/b/1575033906 5
 A1/B1/c/1575033906 3
 A1/B2/a/1575033906 5
 A2/B3/a/1575033906 6
 A1/B2/b/1575033906 7
 A1/B2/c/1575033906 85
 A2/B3/b/1575033906 6
 A2/B3/c/1575033906 4
 .
 .
 .
 A1/B1/a/1575033909 5
 A1/B1/b/1575033909 6
 A1/B1/c/1575033909 4

I want to use a regular expression so that I can read each line of the file in order to split it based on two delimiters. In my case those delimiters are " " and "/". So in the end, I want to have this :

['A1','B1','a','1575033906','4']

Here is the code I used:

for line in f:
    print(line)
    x = re.split(r'[ /]+', line)
    print(x)

And the results it gives me is this:

 ['A1','B1','a','1575033906','4\n']

How could I exclude the "\n" character from getting into that last position?

thelaw
  • 570
  • 4
  • 14

1 Answers1

4

strip or rstrip it away:

x = re.split(r'[ /]+', line.strip())

If there's precious whitespace at the beginning of the line, use rstrip to strip from the right:

>>> ' w t\n'.rstrip()
' w t'
ForceBru
  • 43,482
  • 10
  • 63
  • 98