abcdddd
abcdddd

Reputation: 27

Split list and print desired output

import re

foo='''"a","b","c","aaa":"sdlfjlasjdfljas"'''

list=re.split('\:,\';\"',foo)
i = 0
for word in list:
    newWord = word
    newWord.split(':')

I am currently trying to split the string foo apart with different paramaters like commas, colons, and parenthesis... so on.

I am currently able to split the string once which gives me the string "aaa":"alsjflajlsdjf" all in one index. I want to be able to split it up by the colon and in a if statement find aaa using word.find to get the next string and print it out. So it would print out the gibberish.

I have successfully used a for loop to find the aaa part but I'm not able to print out the gibberish. Thanks for your help in advance.

Upvotes: 1

Views: 112

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180550

You can use str.translate replacing whatever characters you want to split on with a comma and then split on a comma:

tbl= str.maketrans({ord(ch):"," for ch in "();:"})
print("\n".join(foo.translate(tbl).split(",")))

"a"
"b"
"c"
"aaa"
"sdlfjlasjdfljas"

I don't get why you want to split all the words if all you want is the word after aaa, str.find is not going to do much for you if you split the string, you could split from there getting the next word after using find to get the index of aaa.

ind = foo.index("aaa")
if ind != -1:
    ....

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107357

You can simply split your string based on none word characters (\W) :

>>> foo='''"a","b","c","aaa":"sdlfjlasjdfljas"'''
>>> 
>>> re.split(r'\W',foo)
['', 'a', '', '', 'b', '', '', 'c', '', '', 'aaa', '', '', 'sdlfjlasjdfljas', '']

Or for refuse of empty strings you can use re.findall to find the words that contains only word characters (\w):

>>> l=re.findall(r'\w+',foo)
['a', 'b', 'c', 'aaa', 'sdlfjlasjdfljas']

You can use get the first word with length more than one with a generator expression within next function :

>>> next(w for w in l if len(w)>1)
'aaa'

Or use a list comprehension to get all of them :

>>> [w for w in l if len(w)>1]
['aaa', 'sdlfjlasjdfljas']

Note: if you want to split a string with some characters with regex you can use following recipes :

  • use a character class : [:,.;]
  • use logical or : ':|,|\.|;'

But using pip ('|') with re.split may be contains the empty strings.in that case you can use character class.

Upvotes: 2

user1907906
user1907906

Reputation:

Your regular expression should be like this:

[,;:]

The square brackets include all characters you want to split on.

Upvotes: 1

Related Questions