英文:
How to import a range into Python from Excel as a list?
问题
我试图将Excel中的范围A1:C4
导入Python并转换为列表,然后在该列表的每个元素末尾添加'_a'。目前它返回TypeError:"只能连接列表(而不是'str')到列表"
我已经成功地导入了一个范围,然后返回了该范围:
# Excel公式:=funky(A1:C4)
listy = ["item_0"]
@xl_func #在在Excel电子表格中调用的任何函数之前都需要这个
def funky(add): #此函数需要一个输入参数,在这种情况下是在Excel电子表格中给出的范围A1:C4
global listy
listy.extend(add) #将所有传递给函数的元素添加到'listy'中(在这种情况下是Excel电子表格中范围A1:C4的值)
return(listy[1:]) #返回元素[0]之后的所有元素
但它仍然不允许我对范围的值进行任何编辑。
这是我尝试过的另一种方法,使用相同的范围但不同的函数:
@xl_func
def iter(add): #当我将第4行更改为.extend([add])时,我也将'add'更改为'[add]'
list = ["item_0"]
list.append(add) #我也尝试过 .extend(add) 和 .extend([add]) 但是得到了相同的错误
list = [item + '_a' for item in list] #将'_a'添加到'list'中每个项目的末尾
return list #返回编辑后的'list'
Excel公式的样子:
=iter(A1:C4)
这个函数(iter(add)
)返回TypeError。funky(add)
不会返回这个错误。
英文:
I am trying to import the range A1:C4
from Excel into Python as a list, then add '_a' to the end of every element in that list. Currently it is returning the TypeError "can only concatenate list(not 'str') to list"
I've gotten it to import a range before then return that range:
#Excel formula: =funky(A1:C4)
listy = ["item_0"]
@xl_func #needed before any function that is called on in the excel spreadsheet
def funky(add): #needs one input to function, in this case it is being given the range A1:C4 in the excel spreadsheet
global listy
listy.extend(add) #adds all the elements given to the function into 'listy' (in this case it is the values of range A1:C4 in the excel spreadsheet)
return(listy[1:]) #returns all elements past element [0]
but it still doesn't allow me to do any sort of edits to the values of the range.
This is something I've tried to do, with the same range but a different function:
@xl_func
def iter(add): #When I changed line 4 to .extend([add]) I also changed 'add' to '[add]'
list = ["item_0"]
list.append(add) #I also tried .extend(add) and .extend([add]) but got the same error
list = [item + '_a' for item in list] #adds '_a' to the end of every item in 'list'
return list #returns the edited 'list'
What the Excel formula looks like:
=iter(A1:C4)
This function (iter(add)
) is what is returning the TypeError. funky(add)
does not return this error.
答案1
得分: 0
好的,以下是翻译好的部分:
我通过寻找更好的解决方案解决了自己的问题,找到了一个在稍微尝试了一下之后非常有帮助的方法。
我使用 merged = list(itertools.chain.from_iterable(add))
将 add
解散成了一个大列表。
(来源:https://stackoverflow.com/a/953097/21975226)
然后一切都按照预期运行了!
我的主要问题是,我试图将一个字符串添加到列表对象的末尾,这就是为什么它返回 TypeError
的原因。
为了解决这个问题,我将输入列表 (add
) 解散,并使用 .extend()
将其添加到 list
(我将其重命名为 list_0
,以避免与 list(itertools.chain.from_iterable(add))
冲突)。
然后,将字符串 _a
添加到 list_0
中每个元素的行代码完美运行!
英文:
Okay, so I solved my own question by looking around for some better solutions, and I found one that helped a lot after a little bit of messing around with it.
I dissolved add
into a single big list using merged = list(itertools.chain.from_iterable(add))
(Credit: https://stackoverflow.com/a/953097/21975226)
Then everything worked as intended!
My main problem was that I was trying to add a string to the end of a list object, which is why it was returning the TypeError
.
What I did to fix that was dissolve the input list (add
) and use .extend()
to add it to list
(which I renamed to list_0
because of conflict with list(itertools.chain.from_iterable(add))
).
Then the line that adds the string _a
to the end of every element in list_0
worked perfectly!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论