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!

user4659009
  • 675
  • 2
  • 5
  • 19

3 Answers3

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

Pynchia
  • 10,996
  • 5
  • 34
  • 43
  • 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? – user4659009 Oct 26 '15 at 20:33
  • I have tried the code on python 3.4.3 and it works fine. Copy and paste it – Pynchia Oct 26 '15 at 20:36
  • 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? – user4659009 Oct 26 '15 at 20:40
  • maybe it's because the `for` loop is iterating over `bad_csv` instead of `myfile`? – Pynchia Oct 26 '15 at 20:43
  • `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 :/ – user4659009 Oct 26 '15 at 20:44
  • I have never used the csv module yet. I have answered your question regarding the string. Please post a new question in case the subject changes. – Pynchia Oct 26 '15 at 20:48
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
fl00r
  • 82,987
  • 33
  • 217
  • 237
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())
Carson Crane
  • 1,197
  • 8
  • 15