簡體   English   中英

從文本文件python3檢索字典/列表

[英]retrieving dictionary/list from textfile python3

我想將“ coins_info.txt”中的一列值保存到程序中的列表變量中。 以下是“ coins_info.txt”的內容:

Name,ICO,Max,USD_ROI
Bitcoin,0.0,19535.7,N/A
Ethereum,0.0,1389.18,N/A
Ripple,0.0,3.6491,N/A
Bitcoin Cash,0.0,4091.7,N/A
EOS,0.99,21.4637,2068.05%
Litecoin,0.0,366.153,N/A
...

我想從“ coins_info.txt”中的每個硬幣中獲取ICO,並將其保存到如下所示的列表中:

icos = [0.0, 0.0, 0.0, 0.0, 0.99, 0.0, ...]

我嘗試了這段代碼:

import pandas as pd

df = pd.read_csv("coins_info.txt")
icos = df["ICO"].values.tolist()

但是我的代碼第4行出現了此錯誤:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2442, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5280)
  File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5126)
  File "pandas/_libs/hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20523)
  File "pandas/_libs/hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20477)
KeyError: 'ICO'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "temp.py", line 280, in <module>
    init_max_prices("btc.txt", "init")
  File "temp.py", line 212, in init_max_prices
    icos = df["ICO"].values.tolist()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 1964, in __getitem__
    return self._getitem_column(key)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 1971, in _getitem_column
    return self._get_item_cache(key)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/generic.py", line 1645, in _get_item_cache
    values = self._data.get(item)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/internals.py", line 3590, in get
    loc = self.items.get_loc(item)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2444, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5280)
  File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5126)
  File "pandas/_libs/hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20523)
  File "pandas/_libs/hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20477)
KeyError: 'ICO'

我該如何解決我的代碼?

您可以在熊貓中加載txt文件並將ICO列轉換為列表,這是完整的代碼:

import pandas as pd
df = pd.read_csv('coins_info.txt', sep=",", header=0)
icos =list(df['ICO']) 
or 
icos = df['ICO'].values.tolist()

輸出:

icos = [0.0, 0.0, 0.0, 0.0, 0.99, 0.0]

您的代碼與示例數據配合良好:

df = pd.read_csv("coins_info.txt")
print (df)
           Name   ICO         Max   USD_ROI
0       Bitcoin  0.00  19535.7000       NaN
1      Ethereum  0.00   1389.1800       NaN
2        Ripple  0.00      3.6491       NaN
3  Bitcoin Cash  0.00   4091.7000       NaN
4           EOS  0.99     21.4637  2068.05%
5      Litecoin  0.00    366.1530       NaN

icos = df["ICO"].values.tolist()
print (icos)
[0.0, 0.0, 0.0, 0.0, 0.99, 0.0]

所以問題是另外一回事。


KeyError:“ ICO”

表示沒有ICO列。

首先檢查列名稱是否存在一些空格或類似的內容:

print (df.columns.tolist())

然后需要:

df.columns = df.columns.str.strip()

或另一個可能的問題是使用不同的分隔符作為默認的sep=','

然后需要:

df = pd.read_csv("coins_info.txt", sep=';')
icos = df["ICO"].values.tolist()

我想從“ coins_info.txt”中的每個硬幣中獲取ICO,並將其保存到如下所示的列表中...

對於孤立地完成此任務,熊貓可能會顯得過大。 您可以使用內置的csv模塊將您的列讀入列表:

import csv
from io import StringIO

mystr = StringIO("""Name,ICO,Max,USD_ROI
Bitcoin,0.0,19535.7,N/A
Ethereum,0.0,1389.18,N/A
Ripple,0.0,3.6491,N/A
Bitcoin Cash,0.0,4091.7,N/A
EOS,0.99,21.4637,2068.05%
Litecoin,0.0,366.153,N/A""")

# replace mystr with open('coins_info.txt', 'r')
with mystr as fin:
    reader = csv.DictReader(fin)
    ico_list = [float(row['ICO']) for row in reader]

print(ico_list)

[0.0, 0.0, 0.0, 0.0, 0.99, 0.0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM