[英]retrieving dictionary/list from textfile python3
I want to save a column of values from "coins_info.txt" into a list variable into my program. 我想将“ coins_info.txt”中的一列值保存到程序中的列表变量中。 Here are the contents of "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
...
I want to get the ICO from every single coin in "coins_info.txt" and save it into a list that looks like this: 我想从“ coins_info.txt”中的每个硬币中获取ICO,并将其保存到如下所示的列表中:
icos = [0.0, 0.0, 0.0, 0.0, 0.99, 0.0, ...]
I tried this code: 我尝试了这段代码:
import pandas as pd
df = pd.read_csv("coins_info.txt")
icos = df["ICO"].values.tolist()
But I got this error for line 4 of my code: 但是我的代码第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'
What can I do to fix my code? 我该如何解决我的代码?
You can load the txt file in pandas and convert the ICO column to list, Here is the complete code: 您可以在熊猫中加载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()
Output: 输出:
icos = [0.0, 0.0, 0.0, 0.0, 0.99, 0.0]
Your code working nice with your sample data: 您的代码与示例数据配合良好:
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]
so problem is something else. 所以问题是另外一回事。
KeyError: 'ICO'
KeyError:“ ICO”
means there is no column ICO
. 表示没有
ICO
列。
First check columns names if some whitespaces or similar: 首先检查列名称是否存在一些空格或类似的内容:
print (df.columns.tolist())
Then need: 然后需要:
df.columns = df.columns.str.strip()
Or another possible problem is different separator as default sep=','
. 或另一个可能的问题是使用不同的分隔符作为默认的
sep=','
。
Then need: 然后需要:
df = pd.read_csv("coins_info.txt", sep=';')
icos = df["ICO"].values.tolist()
I want to get the ICO from every single coin in "coins_info.txt" and save it into a list that looks like this...
我想从“ coins_info.txt”中的每个硬币中获取ICO,并将其保存到如下所示的列表中...
For this task in isolation, Pandas may be overkill. 对于孤立地完成此任务,熊猫可能会显得过大。 You can use the built-in csv module to read your column into a list:
您可以使用内置的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.