Python Forum
remove spaces with exceptions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove spaces with exceptions
#1
Hello!

I have the following problem:

This is the string: asdas asdd sd a2123 3234 "test asd" asda

Is possible to remove all spaces except the spaces between "" ?

Thank you!
Reply
#2
where does this string come from - is it a file
if it comes from a file, you can use csv module, if it's just a string - you can use io.StringIO and then csv module
spam = 'asdas asdd sd a2123 3234 "test asd" asda'

import io
import csv
eggs = io.StringIO(spam)

rdr = csv.reader(eggs, delimiter=' ')
for line in rdr:
    print(line)
Output:
['asdas', 'asdd', 'sd', 'a2123', '3234', 'test asd', 'asda']
you can further process the list to get desired output
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Ok, managed to write some piece of code to do the job:
word = "asdas asdd sd a2123 3234 \"test asd sdd 12 3231  123\" asda"

quo = False
wordT = ""

for letter in word:
        
    if letter == "\"" and quo == False:
        quo = True
        wordT += letter
        continue
        
    if quo == False:
        wordT += letter
    
    if quo == True:
        if letter == " ":
            wordT += "_"
        else:
            wordT += letter
    
    if letter == "\"" and quo == True:
        quo = False

print(wordT.replace(" ","").replace("_"," "))
Output:
asdasasddsda21233234"test asd sdd 12 3231 123"asda
But the solution is not very elegant.
Reply
#4
You could use @buran's solution and just fix the print statement to
print(''.join(line))
Reply
#5
@buran and @pyzyx3qwerty
Thank you all !
I will use the @buran solution.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python, exceptions KingKhan248 6 466 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 516 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 587 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  Looking for advice and Guidance on Exceptions used within Functions paul41 1 570 Nov-14-2019, 12:33 AM
Last Post: Larz60+
  multi-line messages in raised exceptions? Skaperen 3 1,930 Aug-01-2019, 02:17 AM
Last Post: Skaperen
  Change linenumber and filename printed in exceptions like #line in C kryptomatrix 2 840 Jul-12-2019, 06:01 AM
Last Post: Gribouillis
  Creating custom exceptions that co-operate LadySvetlana 4 1,023 Mar-19-2019, 04:24 PM
Last Post: LadySvetlana
  Catching exceptions in embedded code no longer works on 3.7.2? FFMG 5 1,138 Feb-02-2019, 10:15 AM
Last Post: Larz60+
  Problems with not having exceptions crash my script league55 7 1,956 Feb-16-2018, 09:06 PM
Last Post: DeaD_EyE
  raising multiple exceptions Skaperen 10 4,734 Mar-19-2017, 06:32 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020