从文本文件python3检索字典/列表

时间:2018-06-27 03:25:48

标签: python python-3.x pandas csv text-files

我想将“ 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'

该如何解决我的代码?

3 个答案:

答案 0 :(得分:0)

您可以在熊猫中加载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]

答案 1 :(得分: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()

答案 2 :(得分:0)

  

我想从“ coins_info.txt”中的每个硬币中获取ICO,然后   将其保存到如下所示的列表中...

对于孤立地完成此任务,熊猫可能会过分杀伤。您可以使用内置的csv module将您的列读入列表:

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]