I am a little lost here. I assume your data comes from a file? Well, anyway, using assumptions here is some code:
def strip_brackets(txt):
if txt[0] == "[": # remove leading '['
txt = txt[1:]
if txt[-1] == "]": # remove trailing ']'
txt = txt[:-1]
return txt
# assume your data string looks like this
data_raw = """[20]
[35]
[40]
[84]
[100]
[245]
[260]
[300]
[440]
[521]
[650]
"""
data_list = data_raw.split()
# test
print data_list # ['[20]', '[35]', '[40]', '[84]', ...]
# convert to a list of numeric strings
data_list2 = [strip_brackets(x) for x in data_list]
# test
print data_list2 # ['20', '35', '40', '84', ...]
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
Since the data is coming from a raw source (text file, database, user input...) wouldn't it be nice if we didn't assume that the "[" will always be the first character of "txt" and "]" is not always the last character of "txt".
Given that the data can also be of the form " [ 32] " or "[32 ]". To handle such situations here is what I have come up with....
data_raw = """[20 ]
[ 35 ]
[40 ]
[84 ]
[100 ]
[ 245]
[ 260]
[ 300 ]
[ 440 ]
[ 521 ]
[ 650 ]
"""
data_list = data_raw.split()
print data_list
data_list2 = [x.strip(" []") for x in data_list]
print data_list2
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Since the data is coming from a raw source (text file, database, user input...) wouldn't it be nice if we didn't assume that the "[" will always be the first character of "txt" and "]" is not always the last character of "txt".
Given that the data can also be of the form " [ 32] " or "[32 ]". To handle such situations here is what I have come up with....
data_raw = """[20 ]
[ 35 ]
[40 ]
[84 ]
[100 ]
[ 245]
[ 260]
[ 300 ]
[ 440 ]
[ 521 ]
[ 650 ]
"""
data_list = data_raw.split()
print data_list
data_list2 = [x.strip(" []") for x in data_list]
print data_list2
No cigars on that one s.o.s, here is the result I get:
"""
my output -->
['[20', ']', '[', '35', ']', '[40', ']', '[84', ']', ...]
['20', '', '', '35', '', '40', '', '84', '', ...]
"""
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
Just a little tweakery needed:
data_raw = """[20 ]
[ 35 ]
[40 ]
[84 ]
[100 ]
[ 245]
[ 260]
[ 300 ]
[ 440 ]
[ 521 ]
[ 650 ]
"""
list1 = data_raw.split('\n') # <---
print "***list1***\n", list1
list1_mod = [x.strip(' []') for x in list1 if x] # <---
print "***list1_mod***\n", list1_mod
***list1***
['[20 ]', '[ 35 ]', '[40 ]', '[84 ]', '[100 ]', '[ 245]', '[ 260]', '[ 300 ]', '[ 440 ]', '[ 521 ]', '[ 650 ]', '']
***list1_mod***
['20', '35', '40', '84', '100', '245', '260', '300', '440', '521', '650']
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
That will do it Jeff! HeHe, love the word "tweakery".
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
No cigars on that one s.o.s, here is the result
Doesn't make a difference, I don't smoke anyways...:mrgreen:
And btw Jeff, thanks for correcting my code in my absence, would have done that anyways...:p.
Maybe should run my programs before I post them here...
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Welcome S.O.S to the Python crowd. We are all quite nice in Python land, just here to learn more!
Small Miss Steaks are accepted, gives the rest of us the opportunity to show off how smart we are.
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
Welcome S.O.S to the Python crowd. We are all quite nice in Python land, just here to learn more!
Thanks for squeezing me in...:mrgreen:Small Miss Steaks are accepted, gives the rest of us the opportunity to show off how smart we are. Aha..thought something was fishy the way you said "No cigars on that one ~s.o.s~"..:twisted:
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
This was quite a lively discussion!
A question to ghostDog74, can you use regular expression to simply extract the number from each dataline, no matter what the non-numeric stuff is?
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Thanks this info ghostdog! You must be one great regex expert!
re.findall(r"(\d+)",data_raw) works great on raw_data, but when I changed 40 to 40.5 it gave me [..., '40', '5', ...]
Looks like '\d+ only works on integers.
Do you have any suggestions for floats?
Maybe I should start new thread? Did start own thread see "Extract Numbers from Data Stream"
Thanks in advance!
Henri
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184