Home > other >  How can I split a string with no delimeter?
How can I split a string with no delimeter?

Time:07-11

I need to import CSV file which contains all values in one column although it should be on 3 different columns.

The value I want to split is looking like this "2020-12-30 13:17:00Mojito5.5". I want to look like this: "2020-12-30 13:17:00 Mojito 5.5"

I tried different approaches to splitting it but I either get the error " Dataframe object has no attribute 'split' or something similar.

Any ideas how I can split this?

CodePudding user response:

We could use a regex approach here:

inp = "2020-12-30 13:17:00Mojito5.5"
m = re.findall(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})(\w ?)(\d (?:\.\d )?)', inp)
print(m)  # [('2020-12-30 13:17:00', 'Mojito', '5.5')]

CodePudding user response:

Assuming you always want to add spaces around a word without special characters and numbers you can use this regex:

def add_spaces(m):
  return f' {m.group(0)} '

import re
s = "2020-12-30 13:17:00Mojito5.5"
re.sub('[a-zA-Z] ', add_spaces, s)
  • Related