I've written the following function that takes a tab delimited file (as a string) and turns it into a dictionary with an integer as a key and a list of two floats and the value:
def parseResults(self, results):
"""
Build a dictionary of the SKU (as key), current UK price and current Euro price
"""
lines = results.split('\n')
individual_results = []
for i in range(1,len(lines)-1):
individual_results.append(lines[i].split('\t'))
results_dictionary = {}
for i in range(len(individual_results)):
results_dictionary[int(individual_results[i][0])] = [float(individual_results[i][1]), float(individual_results[i][2])]
return results_dictionary
I've been reading about using list comprehension and also dictionary comprehension but I don't really know what the best way to build this is.
I think I can simplify the first list build using:
individual_results = [results.split('\t') for results in lines[1:]]
but I don't then know the best way to create the dictionary. I've got the feeling this might be possible in a neat way without even creating the intermediate list.
Thanks,
Matt
Like this:
import csv
import StringIO
results = "sku\tdelivered-price-gbp\tdelivered-price-euro\tid\n32850238\t15.53\t35.38\t258505\n"
data = list(csv.DictReader(StringIO.StringIO(results), delimiter='\t'))
print(data)
Output:
[{'sku': '32850238', 'delivered-price-euro': '35.38', 'delivered-price-gbp': '15.53', 'id': '258505'}]
Of course, if you can read from an actual file, you can skip the stringIO part.
To build the type of dictionary you want, you would do this:
data = {}
for entry in csv.DictReader(StringIO.StringIO(results), delimiter='\t'):
data[entry['sku']] = [entry['delivered-price-gbp'], entry['delivered-price-euro']]
Or even as a dictionary comprehension:
import csv
import StringIO
results = "sku\tdelivered-price-gbp\tdelivered-price-euro\tid\n32850238\t15.53\t35.38\t258505\n10395850\t35.21\t46.32\t3240582\n"
data = {entry['sku']: [entry['delivered-price-gbp'], entry['delivered-price-euro']]
for entry in csv.DictReader(StringIO.StringIO(results), delimiter='\t')}
print(data)
But that's now getting highly difficult to read.
The output would in those two last cases be:
{'32850238': ['15.53', '35.38'], '10395850': ['35.21', '46.32']}
使用标准库中的CSV模块,它具有直接读取字典csv.DictReader的方法
Try something like this:
In [8]: from collections import defaultdict
In [9]: with open("filename") as f:
...: dic=defaultdict(list)
...: next(f) #skip the first line
...: for line in f:
...: k,v=line.split(None,1)
...: dic[int(k)].extend( map(float,v.split()[:2]) )
...:
In [10]: dic
Out[10]: defaultdict(<type 'list'>, {32850238: [15.53, 35.38]})
Your code can simply be:
def parseResults(self, results):
lines = results.split('\n')
li_results = [x.split('\t') for x in lines]
results_dict = {int(x[0]):map(float,[x[1],x[2]]) for x in li_results[1:]} # skip the header
return results_dict
or if you want it shorter(not recommended):
def parseResults(self, results):
return {int(x[0]):map(float,[x[1],x[2]]) for x in [i.split('\t') for i in results.split('\n')][1:]}
Output(from the string you have given):
{32850238: [15.53, 35.38]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.