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.
我正在使用CSV文件,从中可以获得多个值。例如,带有书籍的文件,可能有多个作者,例如{Ben Norrington | Chad Andersson}。他们一起写了一本书。
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
这是我的代码,它从CSV文件中获取一列。代码是用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"
2 个解决方案
#1
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.
返回打破循环,因此只返回第一个拆分。您必须调整逻辑,以便将拆分添加到数据结构(甚至是简单的字符串),并在for循环后返回整个结构。虽然没有经过测试,但这可以胜任。
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
#2
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.
return语句从函数返回。该功能不会(不能)继续执行。
Since you put a return
statement in a for
loop, when the return is reached the loop no longer continues to process additional data.
由于您在for循环中放置了一个return语句,当到达返回时,循环不再继续处理其他数据。
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)
#1
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.
返回打破循环,因此只返回第一个拆分。您必须调整逻辑,以便将拆分添加到数据结构(甚至是简单的字符串),并在for循环后返回整个结构。虽然没有经过测试,但这可以胜任。
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
#2
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.
return语句从函数返回。该功能不会(不能)继续执行。
Since you put a return
statement in a for
loop, when the return is reached the loop no longer continues to process additional data.
由于您在for循环中放置了一个return语句,当到达返回时,循环不再继续处理其他数据。
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)