1

I'm working with a CSV-file, from which I might get multiple values. For example, a file with books, which might have multiple writers, for example {Ben Norrington|Chad Andersson}. They have together written a book.

In my code, I'm using regular expressions to split by the | and take remove the { and the }. It works fine.

The problem comes when I want to return the names of the writers. I only get the first name, not the second. How do I get both?

This is my code that takes a column from the CSV-file. The code is written in python 2.7

def ifseveral(x):
        if "{" not in x and "(" not in x and x != "NULL":
                return x
        elif "{" in x:
                splits =""
                splits = x.split("|")
                for i in splits:
                        string = i
                        string = re.sub('[{}]', '', string)
                        if "(" in string:
                                splitpar = ""
                                splited = string.split("(")
                                splitpar += splited[0][0:]
                                return splitpar
                        else:
                                **return string** #here is the problem

        else:
                return "No information available"
1
  • You could simplify splitpar = ""; splited = string.split("("); splitpar += splited[0][0:] to just splitpar = string.split("(")[0]. Commented Dec 18, 2015 at 15:09

2 Answers 2

1

Return breaks the loop, therefore only the first split will be returned. You have to adjust your logic so that you add your splits to a datatstructure (or even a simple string) and return the entire structure after the for loop. This could do the job though it's untested.

def ifseveral(x):
        if "{" not in x and "(" not in x and x != "NULL":
                return x
        elif "{" in x:
                splits =""
                splits = x.split("|")
                return_value = ""
                for i in splits:
                        string = i
                        string = re.sub('[{}]', '', string)
                        if "(" in string:
                                splitpar = ""
                                splited = string.split("(")
                                splitpar += splited[0][0:]
                                return splitpar
                        else:
                                return_value += string+" "
                return return_value

        else:
                return "No information available
Sign up to request clarification or add additional context in comments.

Comments

1

A function can return only a single object. That object can be a simple object such as an integer, or a string, or it can be a more complex object such as a list of objects or it can be a generator.

The return statement returns from the function. The function does not (can not) continue executing.

Since you put a return statement in a for loop, when the return is reached the loop no longer continues to process additional data.

One solution: build a list and return it

def ifseveral(x):
    # ...
    result = []
    for string in splits:
        # ...
        if "(" in string:
            splitpar = ""
            splited = string.split("(")
            splitpar += splited[0][0:]
            result.append(splitpar)
        else:
            result.append(string)

    return result

foo = ifseveral("something")
print(foo)
print(len(foo))
for name in foo:
    print("One of the names is", name)

Another solution is for your function to be a generator:

def ifseveral(x):
    # ...
    for string in splits:
        # ...
        if "(" in string:
            splitpar = ""
            splited = string.split("(")
            splitpar += splited[0][0:]
            yield splitpar
        else:
            yield string

    return result

foo = ifseveral("something")
print(foo)
for name in foo:
    print("One of the names is", name)

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.