[英]how convert a string to a list?
我有個問題。 我將我的結果(這是一個嵌套列表)寫在一個 csv 文件中,但我現在沒有它作為列表。 這是一個字符串,我不能把它變成一個列表。
"[['ariba', index ... OriginalText\n100 100 ... \\n\\n\\n\\n\\n\\nintroduction\\nas an ibm applicatio...\n126 126 ... \\n\\n\\n\\n\\n\\n\\n\\n\\n\\njob description\\njob descr...\n166 166 ... ust is looking for a sap s/4hana, ecc, sap scm...\n207 207 ... job title :- sap mdg architect\\nlocation :- ba...\n307 307 ... \\n\\nintroduction\\n\\n\\nas a package consultant ...\n... ... ... ...\n50375 50375 ... accenture is a leading global professional ser...\n50522 50522 ... \\n\\nbh: 3514573\\n\\n\\n***** must be able to wor...\n50559 50559 ... job description\\nfor a \\npromising career deve...\n50616 50616 ... \\n\\n\\n\\n\\n\\n\\n\\n\\n\\njob description\\na leading...\n50830 50830 ... stefanini is looking for a enterprise architec...\n\n[963 rows x 4 columns], 0.03690168409406433], ['excel dashboards', index ... OriginalText\n67 67 ... description\\nabout lumen\\nlumen is guided by o...\n167 167 ... the panum group is looking for an energetic en...\n188 188 ... at pitcher, we thrive on innovation\\n\\n\\n\\npit...\n240 240 ... company description\\nas one of the largest ind...\n252 252 ... founded in hong kong since 2003 to serve busin...\n... ... ... ...\n50518 50518 ... deloitte is the largest private professional s...\n50667 50667 ... the \\nenterprise architect (ea)\\n works with s...\n50780 50780 ... \\n\\nat basin electric, our employees are the h...\n50948 50948 ... \\n\\n\\n\\n\\n\\n\\n\\n\\n\\njob summary\\nthe sr. solut...\n50972 50972 ... \\n\\n\\n\\n\\n\\n\\n\\n\\n morgan, lewis & bock...\n\n[769 rows x 4 columns], 0.038849737036863105]]"
以上只是我列表中的兩個元素。 如您所見,每個子列表的第二個元素非常混亂,我找不到任何東西將其拆分為一個列表。 上面的示例具有以下結構:
"[['A','B'],['C','D']]"
正如我提到的B
和D
很復雜? 有什么方法可以執行此轉換?
如果您可以提前拼出列表中的所有字符串,則可以使用ast.literal_eval()
將字符串轉換為 Python 列表。 不要像評論中建議的那樣使用eval()
: 它很危險。
import ast
ast.literal_eval("[['A','B'],['C','D']]")
這輸出:
[['A', 'B'], ['C', 'D']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.