3

i use python to read a txt file.

i have tried using .lstrip to remove the whitespace from the left but it only manages to remove the whitespace from the first line.

In the text file:

when i use this:

puzzle = puzzle.replace('',' ')

 x m f y c x v t l j l q b b y b k o u m j q w b t 
 c a u b m e k n b e y d q m c n z y j p v r a n k 
 a q a c t i v e x n y v w d v c o s h o y a o h g 
 p a g h z k c t u d p t j d p h s z t p r h t t l 
 s b s n a k j w q b o u f t m g n j q b t l i n u 
 t s e w o h v o b d s d u q j i f f k y y l o d o 
 o u k w w e f r o y a m a p m l r r p v d o l o p 
 c q k f x t l k s j v t m t r s b y c m q r r r i 
 k f e r v l q i d q a x a o a n f q j l m c p j h 
 y o y y w r b p f c j l f b c b b c o e c s p w l 
 t w b x e t y u y u f v v m a u a w j m b w l q h 
 t x o k d e x m d b t g v h p s v s q t m l j d x 
 d c a t e n r e h t e o x q d g e u e l j t r r n 
 j a r t e q v t x e j f s q d d k b u h c y s f q 
 h p d r o w s s a p x t r x h p d x c d h i c o n

what i get when i use: print (puzzle.lstrip())

x m f y c x v t l j l q b b y b k o u m j q w b t 
 c a u b m e k n b e y d q m c n z y j p v r a n k 
 a q a c t i v e x n y v w d v c o s h o y a  t j d p h s z t p r h t t l 
 s b s n a k j w q b o u f t m g n j q b t l i n u 
 t s e w o h v o b d s d u q j i f f k y y l o d o 
 o u k w w e f r o y a m a p m l r r p v d o l o p 
 c q k f x t l k s j v t m t r s b y c m q r r r i 
 k f e r v l q i d q a x a o a n f q j l m c p j h 
 y o y y w r b p f c j l f b c b b c o e c s p w l 
 t w b x e t y u y u f v v m a u a w j m b w l q h 
 t x o k d e x m d b t g v h p s v s q t m l j d x 
 d c a t e n r e h t e o x q d g e u e l j t r r n 
 j a r t e q v t x e j f s q d d k b u h c y s f q 
 h p d r o w s s a p x t r x h p d x c d h i c o n 

Instead i want remove all the whitespace from the left to get:

x m f y c x v t l j l q b b y b k o u m j q w b t 
c a u b m e k n b e y d q m c n z y j p v r a n k 
a q a c t i v e x n y v w d v c o s h o y a o h g 
p a g h z k c t u d p t j d p h s z t p r h t t l 
s b s n a k j w q b o u f t m g n j q b t l i n u 
t s e w o h v o b d s d u q j i f f k y y l o d o 
o u k w w e f r o y a m a p m l r r p v d o l o p 
c q k f x t l k s j v t m t r s b y c m q r r r i 
k f e r v l q i d q a x a o a n f q j l m c p j h 
y o y y w r b p f c j l f b c b b c o e c s p w l 
t w b x e t y u y u f v v m a u a w j m b w l q h 
t x o k d e x m d b t g v h p s 

Thanks for your help!!!!

2
  • the input file you have posted does not seem to contain a whitespace(s) at all Commented Dec 21, 2017 at 10:47
  • 1
    Why do you execute this first? : puzzle = puzzle.replace('',' ') Commented Dec 21, 2017 at 10:55

3 Answers 3

2

Starting from txt

txt = """xmfycxvtljlqbbybkoumjqwbt
caubmeknbeydqmcnzyjpvrank
aqactivexnyvwdvcoshoyaohg
paghzkctudptjdphsztprhttl
sbsnakjwqbouftmgnjqbtlinu
tsewohvobdsduqjiffkyylodo
oukwwefroyamapmlrrpvdolop
cqkfxtlksjvtmtrsbycmqrrri
kfervlqidqaxaoanfqjlmcpjh
yoyywrbpfcjlfbcbbcoecspwl
twbxetyuyufvvmauawjmbwlqh
txokdexmdbtgvhpsvsqtmljdx
dcatenrehteoxqdgeueljtrrn
jarteqvtxejfsqddkbuhcysfq
hpdrowssapxtrxhpdxcdhicon"""

you can print the desired result like that:

for line in txt.splitlines():
    print(' '.join(line))

and if you don't want to print, you can use a list comprehension to store the results in a variable:

result = [' '.join(line) for line in txt.splitlines()]
Sign up to request clarification or add additional context in comments.

2 Comments

I think he wants to remove all whitespaces, instead of adding a whitespace after every line
@fechnert This is an alternative way to add the desired whitespaces instead of replace('', ' '), not for removing the leading space on each line (though that could be made clearer in the answer)
0

You can replace all instances of newline followed by space by newline:

puzzle = puzzle.replace('\n ','')

Comments

0

I suppose this might work:

result = [row.lstrip() for row in filehandle.readlines()]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.