Skip to content
Advertisement

replace whitespace with comma in multiline string (doc string), but keeping end-of-line

I have a multiline string (and not a text file) like this:

JavaScript

The column white spaces are unequal.

I want to replace the whitespace with a comma, but keep the end-of-line.

So the result would look like this:

JavaScript

…or alternatively as a pandas dataframe.

what i have tried

  • I can use replace('') with different spaces, but need to count the white spaces
  • I can use the re module (from here re.sub question ), but it converts the whole string to 1 line, where as i need to keep multiple lines (end-of-line).

Advertisement

Answer

Try with StringIO

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement