使用python将csv文件转换为元组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18776370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a csv file into a list of tuples with python
提问by Sean
I am to take a csv with 4 columns: brand, price, weight, and type.
我要带 4 列的 csv:品牌、价格、重量和类型。
The types are orange, apple, pear, plum.
种类有橙子、苹果、梨子、李子。
Parameters: I need to select the most possible weight, but by selecting 1 orange, 2 pears, 3 apples, and 1 plum by not exceeding as $20 budget. I cannot repeat brands of the same fruit (like selecting the same brand of apple 3 times, etc).
参数:我需要选择最大可能的重量,但选择 1 个橙子、2 个梨、3 个苹果和 1 个李子,预算不超过 20 美元。我不能重复同一水果的品牌(例如选择同一品牌的苹果 3 次等)。
I can open and read the csv file through Python, but I'm not sure how to create a dictionary or list of tuples from the csv file?
我可以通过 Python 打开和读取 csv 文件,但我不确定如何从 csv 文件创建字典或元组列表?
For more clarity, here's an idea of the data.
为了更清楚,这里有一个数据的概念。
Brand, Price, Weight, Type
brand1, 6.05, 3.2, orange
brand2, 8.05, 5.2, orange
brand3, 6.54, 4.2, orange
brand1, 6.05, 3.2, pear
brand2, 7.05, 3.6, pear
brand3, 7.45, 3.9, pear
brand1, 5.45, 2.7, apple
brand2, 6.05, 3.2, apple
brand3, 6.43, 3.5, apple
brand4, 7.05, 3.9, apple
brand1, 8.05, 4.2, plum
brand2, 3.05, 2.2, plum
Here's all I have right now:
这是我现在所拥有的:
import csv
test_file = 'testallpos.csv'
csv_file = csv.DictReader(open(test_file, 'rb'), ["brand"], ["price"], ["weight"], ["type"])
回答by Joran Beasley
import csv
with open("some.csv") as f:
r = csv.reader(f)
print filter(None,r)
or with list comprehension
或使用列表理解
import csv
with open("some.csv") as f:
r = csv.reader(f)
print [row for row in r if row]
for comparison
比较
In [3]: N = 100000
In [4]: the_list = [randint(0,3) for _ in range(N)]
In [5]: %timeit filter(None,the_list)
1000 loops, best of 3: 1.91 ms per loop
In [6]: %timeit [i for i in the_list if i]
100 loops, best of 3: 4.01 ms per loop
[edit] since your actual output does not have blanks you donot need the list comprehension or the filter you can just say list(r)
[编辑] 由于您的实际输出没有空格,您不需要列表理解或过滤器,您可以只说 list(r)
Final answer without blank lines
没有空行的最终答案
import csv
with open("some.csv") as f:
print list(csv.reader(f))
if you want dicts you can do
如果你想要 dicts 你可以做
import csv
with open("some.csv") as f:
reader = list(csv.reader(f))
print [dict(zip(reader[0],x)) for x in reader]
#or
print map(lambda x:dict(zip(reader[0],x)), reader)
回答by dawg
You can ponder this:
你可以这样思考:
import csv
def fitem(item):
item=item.strip()
try:
item=float(item)
except ValueError:
pass
return item
with open('/tmp/test.csv', 'r') as csvin:
reader=csv.DictReader(csvin)
data={k.strip():[fitem(v)] for k,v in reader.next().items()}
for line in reader:
for k,v in line.items():
k=k.strip()
data[k].append(fitem(v))
print data
Prints:
印刷:
{'Price': [6.05, 8.05, 6.54, 6.05, 7.05, 7.45, 5.45, 6.05, 6.43, 7.05, 8.05, 3.05],
'Type': ['orange', 'orange', 'orange', 'pear', 'pear', 'pear', 'apple', 'apple', 'apple', 'apple', 'plum', 'plum'],
'Brand': ['brand1', 'brand2', 'brand3', 'brand1', 'brand2', 'brand3', 'brand1', 'brand2', 'brand3', 'brand4', 'brand1', 'brand2'],
'Weight': [3.2, 5.2, 4.2, 3.2, 3.6, 3.9, 2.7, 3.2, 3.5, 3.9, 4.2, 2.2]}
If you want the csv file literally as tuples by rows:
如果您希望 csv 文件实际上是按行元组:
import csv
with open('/tmp/test.csv') as f:
data=[tuple(line) for line in csv.reader(f)]
print data
# [('Brand', ' Price', ' Weight', ' Type'), ('brand1', ' 6.05', ' 3.2', ' orange'), ('brand2', ' 8.05', ' 5.2', ' orange'), ('brand3', ' 6.54', ' 4.2', ' orange'), ('brand1', ' 6.05', ' 3.2', ' pear'), ('brand2', ' 7.05', ' 3.6', ' pear'), ('brand3', ' 7.45', ' 3.9', ' pear'), ('brand1', ' 5.45', ' 2.7', ' apple'), ('brand2', ' 6.05', ' 3.2', ' apple'), ('brand3', ' 6.43', ' 3.5', ' apple'), ('brand4', ' 7.05', ' 3.9', ' apple'), ('brand1', ' 8.05', ' 4.2', ' plum'), ('brand2', ' 3.05', ' 2.2', ' plum')]