Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 replace white space with a string, is this pythonic?
#1
i need to replace a run of white space characters by a string, which is usually a single character (but not always). for example: "foo \t bar" -> "foo_bar". is this a pythonic way to do that?
    replace_with = "_" # or this gets set some other way
    convert_this = "foo \t bar" # or this gets set some other way
    result = replace_with.join(convert_this.split())
or is there a better one like a not-known-to-me way to specify a run of white space in str.replace()?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
basically yes
>>> s = 'foo \t bar'.split()
>>> s
['foo', 'bar']
>>> '_'.join(s)
'foo_bar'
Skaperen likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  which is "better" (or more Pythonic)? Skaperen 8 287 Nov-16-2019, 06:46 PM
Last Post: Skaperen
  Replace string in many files in a folder metro17 8 357 Oct-16-2019, 06:46 PM
Last Post: ndc85430
  White spaces kdiba 1 163 Oct-08-2019, 06:52 PM
Last Post: Aurthor_King_of_the_Brittons
  open, read and replace a string in a file Reims 0 151 Oct-02-2019, 01:30 PM
Last Post: Reims
  Read each line, replace string and save into a new file igormonteiro 2 284 Sep-15-2019, 01:24 PM
Last Post: buran
  which is more Pythonic? Skaperen 5 312 Jul-16-2019, 01:00 AM
Last Post: Skaperen
  including the white space parts in str.split() Skaperen 6 415 Jun-20-2019, 06:03 PM
Last Post: Skaperen
  Replace a list or string element with a random one.. pianistseb 3 344 May-09-2019, 08:24 AM
Last Post: buran
  Replace changing string including uppercase character with lowercase character silfer 11 875 Mar-25-2019, 12:54 PM
Last Post: silfer
  Word replace with string dabozz 1 395 Feb-13-2019, 03:11 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)