Skip to main content
Minor corrections
Source Link
dragosht
  • 3.3k
  • 2
  • 26
  • 33

IḿI'm working with a csvCSV-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}{Ben Norrington|Chad Andersson}. They have togehtertogether written a book.

In my code, Iḿ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 csvCSV-file. The code is written in python 2.7python 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"

Iḿ 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 togehter written a book.

In my code, Iḿ 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"

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"

Iḿ 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 togehter written a book.

In my code, Iḿ 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

            return splitpar
       else:
            return string

    elif "(" in xelse:
            splitt = ""
            splitted = x.split("(")
         **return string** #here splittis +=the splitted[0][0:]problem
      
       return splitt
else:
    else:
            return "No information available"

Iḿ 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 togehter written a book.

In my code, Iḿ 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

    elif "(" in x:
            splitt = ""
            splitted = x.split("(")
            splitt += splitted[0][0:]
            return splitt

    else:
            return "No information available"

Iḿ 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 togehter written a book.

In my code, Iḿ 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"
Source Link

Returning multiple values in python, from csv

Iḿ 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 togehter written a book.

In my code, Iḿ 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

    elif "(" in x:
            splitt = ""
            splitted = x.split("(")
            splitt += splitted[0][0:]
            return splitt

    else:
            return "No information available"