使用python将csv文件转换为元组列表 [英] Converting a csv file into a list of tuples with python

查看:3058
本文介绍了使用python将csv文件转换为元组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这些类型是橙子,苹果,梨子,李子。 p>

参数:我需要选择最可能的重量,但通过选择1橙色,2梨,3苹果和1梅花不超过$ 20预算。我不能重复品牌的同一水果(像选择相同的品牌的苹果3次,等)。



我可以通过Python打开和读取csv文件,但我不知道如何从csv文件创建字典或元组列表?



为了更清楚,这里是数据的概念。

 品牌,价格,重量,类型
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

这里是我现在有的:

  import csv 
test_file ='testallpos.csv'
csv_file = csv.DictReader(open(test_file,'rb'), brand],[price],[weight],[type])


解决方案

您可以思考:

  import csv 
$ b b def fitem(item):
item = item.strip()
try:
item = float(item)
except ValueError:
pass
返回项

with open('/ tmp / test.csv','r')as csvin:
reader = csv.DictReader(csvin)
data = {k。阅读器中的行:
对于line.items()中的k,v:

k = k.strip()
data [k] .append(fitem(v))

打印数据

列印:

  {'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],
'类型':['orange','orange','orange','pear','pear' '品牌','品牌','品牌','品牌','品牌','品牌','品牌' '品牌2','品牌3','品牌1','品牌2','品牌3','品牌4','品牌1','品牌2'],
' 4.2,3.2,3.6,3.9,2.7,3.2,3.5,3.9,4.2,2.2]}

如果你想要csv文件按行按元组:

  import csv 
with open('/ tmp / test .csv')as f:
data = [tuple(line)for line in csv.reader(f)]

打印数据
#[('Brand' Price','Weight','Type'),('brand1','6.05','3.2','orange'),('brand2','8.05','5.2','orange'品牌3','6.54','4.2','橙'),('brand1','6.05','3.2','pear'),'brand2','7.05' ('brand1','5.45','2.7','apple'),('brand2','6.05','3.2' ('brand4','7.05','3.9','apple'),('brand3','6.43','3.5' ,'4.2','plum'),('brand2','3.05','2.2','plum')]


I am to take a csv with 4 columns: brand, price, weight, and type.

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).

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?

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"])

解决方案

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:

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')]

这篇关于使用python将csv文件转换为元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆