bbm
bbm

Reputation: 43

Splitting strings at multiple delimiters in python

I have a file text.txt which contains the ff:

apple boy 'cat'
dog, egg fin
goat hat ice!

I need to split the text file using spaces and special characters and while also ignoring new lines so that the output will be an array like this:

["apple", "boy", "'", "cat", "'", "dog", "egg", "fin", "goat", "hat", "ice", "!"]

but so far the output of my code results to something like this: it returns the string per character and even retains the spaces...

["a", "p", "p", "l", "e", "b", "o", "y", "'", "c", "a", "t", "'", "\n," "d", "o", "g", "e", "g", "g", "f", "i", "n", "\n", "g", "o", "a", "t", "h", "a", "t", "i", "c", "e", "!", "\n" ] 

Here is my code:

file=open(text.txt)

for i in file:
        i.split(" ")
        b+=i


print b

what to do if importing of any modules is not allowed? especially the re module?

Upvotes: 2

Views: 770

Answers (2)

raymelfrancisco
raymelfrancisco

Reputation: 871

Put all the lines to a list of individual characters ( l ).

Then check if a character is an alphanumeric or not, while the current char is an alphanumeric ( isalnum() ), it is combined to a string ( comb ), this string is added to the output list ( out ) when a non-alphanumeric is found, then adds the non-alphanumerics separately until an alphanumeric is found again.

out is then filtered from newlines and spaces.

with open('text.txt') as f:
    l = f.readlines()

    # separates each character into a list
    l = list(l)

    # output list
    out = []

    # string in which alphanumerics will be combined
    comb = ''

    # loops through chars
    # comb is added with chars while char is alphanumberic,
    # comb is added to out when a non-alphanumeric char is detected 
    # and then it resets, and the char detected as punc is added as well
    for ch in l:
        if ch.isalnum():
            comb += ch
        else:
            out.append(comb)
            out.append(ch)
            comb = ''

    # filters out from space and newlines
    out = [ s for s in out if s != '' and s != '\n' and s != ' ' ]

    print(out)

Output:

['apple', 'boy', "'", 'cat', "'", 'dog', ',', 'egg', 'fin', 'goat', 'hat', 'ice', '!']

Upvotes: 1

Padraic Cunningham
Padraic Cunningham

Reputation: 180532

Use a temp string, find the non-alphanumeric characters wrapping them in spaces both side then split at the end

lines ="""apple boy 'cat'
dog, egg fin
goat hat ice!"""

out = []
for line in lines.splitlines():
    temp = ""
    for ch in line:
        if ch.isalnum():
            temp+= ch
        else:
            temp += " {} ".format(ch)
    out.extend(temp.split())
print(out)

Output:

['apple', 'boy', "'", 'cat', "'", 'dog', ',', 'egg', 'fin', 'goat', 'hat', 'ice', '!']

Using your file is just a matter of iterating over the file object and applying the same logic:

with open("text.txt") as f:
    out = []
    for line in f:
        temp = ""
        for ch in line:
            if ch.isalnum():
                temp += ch
            else:
                temp += " {} ".format(ch)
        out.extend(temp.split())

You could also use a set of punctuation chars and change the logic checking if a char appears in the set or not:

st = set("""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""")
with open("text.txt") as f:
    out = []
    for line in f:
        temp = ""
        for ch in line:
            if ch not in st:
                temp += ch
            else:
                temp += " {} ".format(ch)
        out.extend(temp.split())

Upvotes: 3

Related Questions