英文:
How to read excel cell values as a list?
问题
以下是代码部分的中文翻译:
# 读取Excel文件
dataframe1 = pd.read_excel('Data.xlsx')
下面的代码行给我一个字符串列表,这是有意义的。
# 读取mega_links列
mega_linkss = dataframe1.iloc[:,7].tolist()
for link in mega_linkss:
print(link, ' ', type(link))
break
输出:
"[https://mega.nz/folder/KXICCZrS#dJT2bqTh3eK_xbbMPuNglQ]" <class 'str'>
如您所见,类型是 'str'。
为了使类型为 'list',我尝试以下代码。
# 当我使用list函数将字符串转换为列表时
for link in mega_linkss:
print(list(link))
break
输出:
['[', "'", 'h', 't', 't', 'p', 's', ':', '/', '/', 'm', 'e', 'g', 'a', '.', 'n', 'z', '/', 'f', 'o', 'l', 'd', 'e', 'r', '/', 'K', 'X', 'I', 'C', 'C', 'Z', 'r', 'S', '#', 'd', 'J', 'T', '2', 'b', 'q', 'T', 'h', '3', 'e', 'K', '_', 'x', 'b', 'b', 'M', 'P', 'u', 'N', 'g', 'l', 'Q', "'", ']']
我需要输出如下所示。
输出:
['https://mega.nz/folder/KXICCZrS#dJT2bqTh3eK_xbbMPuNglQ'] <class 'list'>
附有Excel表格截图。
为了更好地理解我需要的内容,附有代码截图。
谢谢。
英文:
When I tried to read data from excel file (excell sheet screenshot attached), I get values as strings. But i need python datatype when i read excel file data.
#read excel file
dataframe1 = pd.read_excel('Data.xlsx')
Below code line give me list of string that makes sense.
#read mega_links column
mega_linkss = dataframe1.iloc[:,7].tolist()
for link in mega_linkss:
print(link, ' ',type(link))
break
OUTPUT:
"['https://mega.nz/folder/KXICCZrS#dJT2bqTh3eK_xbbMPuNglQ']" <class 'str'>
As you can see class is 'str'
To get class as 'list', i tried following code.
# when i used list function to convert string in list.
for link in mega_linkss:
print(list(link))
break
OUTPUT:
['[', "'", 'h', 't', 't', 'p', 's', ':', '/', '/', 'm', 'e', 'g', 'a', '.', 'n', 'z', '/', 'f', 'o', 'l', 'd', 'e', 'r', '/', 'K', 'X', 'I', 'C', 'C', 'Z', 'r', 'S', '#', 'd', 'J', 'T', '2', 'b', 'q', 'T', 'h', '3', 'e', 'K', '_', 'x', 'b', 'b', 'M', 'P', 'u', 'N', 'g', 'l', 'Q', "'", ']']
I need output like this.
OUTPUT:
['https://mega.nz/folder/KXICCZrS#dJT2bqTh3eK_xbbMPuNglQ'] <class 'list'>
Excell Sheet Screenshot is attached.
Code Screenshot is attached for a better understanding of what I need.
Thanks
[enter image description here](https://i.stack.imgur.com/yLsK8.jpg)
I need output like this.
OUTPUT:
['https://mega.nz/folder/KXICCZrS#dJT2bqTh3eK_xbbMPuNglQ'] <class 'list'>
答案1
得分: 1
你的Excel文件中的值是一个字符串 "['https...']"。它是列表的字符串表示,所以如果你想将其转换为列表,可以使用 eval(string)
或 ast.literal_eval(string)
。
eval
被认为是不安全的,而 ast.literal_eval
则是一个更安全的选择,它是内置的 ast
模块的一部分。
例如,ast.literal_eval(""[1,2,3]"")
= [1, 2, 3]
。同样,ast.literal_eval(link)
将返回一个包含你的链接的字符串列表,根据你的Excel表格,它将是一个包含单个字符串的列表。
英文:
The value in your excel file is a string "['https...']". It is a string representation of a list, so if you wanted to convert it to a list, you could use eval(string)
or ast.literal_eval(string)
.
eval
is considered unsafe, and ast.literal_eval
is a safer alternative, part of a built-in ast
module.
For example, ast.literal_eval("[1,2,3]")
= [1, 2, 3]
. Likewise, ast.literal_eval(link)
will return a list version of your link, which according to your excel table will be a list with a single string in it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论