0

Tried a different number of combinations, none to avail.

Basically this is the string of numbers that I have:

20101002  100224   1    1044      45508  1001  1002  1003  1004  1005  1006

Output I'm after:

20101002 100224 1 1044 45508 1001 1002 1003 1004 1005 1006

Basically all whitespace before and after the last number have to be trimmed. On top of that there should only be one whitespace between the numbers.

I should add that the code I currently have is as follows and only deals with the whitespace at the start and end of the string:

row = line.strip().split(' ')

Any help is much appreciated, thanks!

2
  • Did you try anything? Commented Oct 26, 2015 at 20:30
  • use a regex and re.sub() method Commented Oct 26, 2015 at 20:31

3 Answers 3

5

this

s = '20101002  100224   1    1044      45508  1001  1002  1003  1004  1005  1006'
new_s = ' '.join(s.split())
print(new_s)

produces

20101002 100224 1 1044 45508 1001 1002 1003 1004 1005 1006

Basically, there are two steps involved:

first we split the string into words with s.split(), which returns this list

['20101002', '100224', '1', '1044', '45508', '1001', '1002', '1003', '1004', '1005', '1006']

then we pass the list to ' '.join, which joins all the elements of the list using the space character between them

Sign up to request clarification or add additional context in comments.

6 Comments

I get ` , , , ,2,0,1,0,1,0,0,2, , ,1,0,0,2,2,4, , , ,1, , , , ,1,0,4,4, , , , , , ,4,5,5,0,8, , ,1,0,0,1, , ,1,0,0,2, , ,1,0,0,3, , ,1,0,0,4, , ,1,0,0,5, , ,1,0,0,6," "` back. Could it be because I'm using Python 3?
I have tried the code on python 3.4.3 and it works fine. Copy and paste it
it looks good when I print it on terminal but it changes format when I export it to a csv :/ with open(bad_csv_file, 'w') as myfile: wr = csv.writer(myfile) for line in bad_csv: row = ' '.join(line.split()) print(row) wr.writerow(row) Any ideas why?
maybe it's because the for loop is iterating over bad_csv instead of myfile?
fname = "track_param_stoch.txt" bad_csv_file = 'bad_hist_csv.csv' bad_csv = [] with open(fname) as f: lines = f.readlines() for line in lines: good_csv.append(line) if pattern.match(line) else bad_csv.append(line) it's iterating correctly, guess this is a output to csv problem not a strip whitespace one :/
|
3
import re
text = "20101002  100224   1    1044      45508  1001  1002  1003  1004  1005  1006"
re.sub(r"\s+", " ", text)
#=> 20101002 100224 1 1044 45508 1001 1002 1003 1004 1005 1006

Comments

0

By default Python's str.split method splits on whitespace.

a = "20101002  100224   1    1044      45508  1001  1002  1003  1004  1005  1006"

" ".join(a.split())

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.