如何将Excel单元格的值读取为列表?

huangapple go评论86阅读模式
英文:

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(&#39;Data.xlsx&#39;)

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, &#39;   &#39;,type(link))
    break

OUTPUT:

&quot;[&#39;https://mega.nz/folder/KXICCZrS#dJT2bqTh3eK_xbbMPuNglQ&#39;]&quot;     &lt;class &#39;str&#39;&gt;
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:

[&#39;[&#39;, &quot;&#39;&quot;, &#39;h&#39;, &#39;t&#39;, &#39;t&#39;, &#39;p&#39;, &#39;s&#39;, &#39;:&#39;, &#39;/&#39;, &#39;/&#39;, &#39;m&#39;, &#39;e&#39;, &#39;g&#39;, &#39;a&#39;, &#39;.&#39;, &#39;n&#39;, &#39;z&#39;, &#39;/&#39;, &#39;f&#39;, &#39;o&#39;, &#39;l&#39;, &#39;d&#39;, &#39;e&#39;, &#39;r&#39;, &#39;/&#39;, &#39;K&#39;, &#39;X&#39;, &#39;I&#39;, &#39;C&#39;, &#39;C&#39;, &#39;Z&#39;, &#39;r&#39;, &#39;S&#39;, &#39;#&#39;, &#39;d&#39;, &#39;J&#39;, &#39;T&#39;, &#39;2&#39;, &#39;b&#39;, &#39;q&#39;, &#39;T&#39;, &#39;h&#39;, &#39;3&#39;, &#39;e&#39;, &#39;K&#39;, &#39;_&#39;, &#39;x&#39;, &#39;b&#39;, &#39;b&#39;, &#39;M&#39;, &#39;P&#39;, &#39;u&#39;, &#39;N&#39;, &#39;g&#39;, &#39;l&#39;, &#39;Q&#39;, &quot;&#39;&quot;, &#39;]&#39;]

I need output like this.

OUTPUT:

[&#39;https://mega.nz/folder/KXICCZrS#dJT2bqTh3eK_xbbMPuNglQ&#39;]     &lt;class &#39;list&#39;&gt;

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:

[&#39;https://mega.nz/folder/KXICCZrS#dJT2bqTh3eK_xbbMPuNglQ&#39;]     &lt;class &#39;list&#39;&gt;

答案1

得分: 1

你的Excel文件中的值是一个字符串 "['https...']"。它是列表的字符串表示,所以如果你想将其转换为列表,可以使用 eval(string)ast.literal_eval(string)

eval 被认为是不安全的,而 ast.literal_eval 则是一个更安全的选择,它是内置的 ast 模块的一部分。

例如,ast.literal_eval("&quot;[1,2,3]&quot;") = [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(&quot;[1,2,3]&quot;) = [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.

huangapple
  • 本文由 发表于 2023年2月18日 18:55:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定