Im trying to collect info from a csv file and turn the row values into variables and carry those variables into different def functions finding such variables such as the max of whats inside and basically gather information from the csv.
Simple, you can use the csv module
for example, have the following csv file
a,1
b,7
c,5
d,2
e,6
>>> import csv
>>> filename = "/Users/sunnky/Desktop/test.csv"
>>> d = {}
>>> with open(filename, mode='r', encoding='utf-8-sig') as f:
... reader = csv.reader(f)
... for k, v in reader:
... d[k] = v
...
>>> new_d = sorted(d.items(), key = lambda m: m[1])
>>> min, max = new_d[0], new_d[len(new_d)-1]
>>> print(min, max)
('a', '1') ('b', '7')
Related
I'm trying to pickle high scores and then print them.
In the actual program, score is acquired from a simple trivia game.
score = 10
name = input("Name: ")
scores = [name, score]
high_scores = open("high_scores.dat", "ab")
pickle.dump(scores, high_scores)
high_scores.close()
high_scoresR = open("high_scores.dat", "rb")
results = pickle.load(high_scoresR)
print(results)
high_scores.close()
The program prints only the first high score entered, it doesn't matter how many scores I try to dump to it. Example:
['Jason', 10]
I am guessing I don't understand something quite basic, so I would highly appreciate a informative and clear explanation.
You can use 'wb' mode to write multiple pickles to a file, and if you need to reopen it for one ore more additional dump, then you should use append mode ('a', not 'w'). Here I write multiple entries using 'wb', and then later add one entry using 'ab'.
>>> scores = dict(Travis=100, Polly=125, Guido=69)
>>> import pickle
>>> with open('scores.pkl', 'wb') as highscores:
... for name,score in scores.items():
... pickle.dump((name,score)), highscores)
...
>>> with open('scores.pkl', 'ab') as highscores:
... pickle.dump(scores, highscores)
...
>>> with open('scores.pkl', 'rb') as highscores:
... a = pickle.load(highscores)
... b = pickle.load(highscores)
... c = pickle.load(highscores)
... d = pickle.load(highscores)
...
>>> a
('Travis', 100)
>>> b
('Polly', 125)
>>> c
('Guido', 69)
>>> d
{'Polly': 125, 'Travis': 100, 'Guido': 69}
>>>
And if you have a lot of data, so that you are worried about being able to dump and/or load all of your items at once, then you can use (one of my packages) klepto, which enables you to store large pickled data to a file, directory, or database… where you can seamlessly access one entry at a time.
>>> import klepto
>>> store = klepto.archives.dir_archive('high', serialized=True)
>>> store.update(scores)
>>> store
dir_archive('high', {'Polly': 125, 'Guido': 69, 'Travis': 100}, cached=True)
>>> # dump all entries at once
>>> store.dump()
>>> # create a new empty archive proxy
>>> store2 = klepto.archives.dir_archive('high', serialized=True)
>>> store2
dir_archive('high', {}, cached=True)
>>> # load one entry, as opposed to loading all entries
>>> store2.load('Guido')
>>> store2
dir_archive('high', {'Guido': 69}, cached=True)
>>> store2['Guido']
69
>>> # load everything else
>>> store2.load()
>>> store2
dir_archive('high', {'Polly': 125, 'Guido': 69, 'Travis': 100}, cached=True)
>>>
You could read your file into a dictionary:
name = input('Enter name: ')
score = input('Enter score: ')
# write new entry to file
with open('highscores.txt', 'a') as f:
f.write(name + ':' + score + '\n')
# read file into dict
with open('highscores.txt', 'r') as f:
lines = f.read().splitlines()
scores = dict(line.split(':') for line in lines)
for key, value in scores.items():
print(key, value)
I didn't know you were trying to learn pickle, but maybe this helps someone else.
I have a dictionary in python in this way
my_dict = {'1':['a','b','c'], '2':['d','e','f']}
and i want to write a csv file in which it is displayed as it follows
1, a b c
2, d e f
because it is parsed by another application in this specific format.
Is there any way to it?
This is a way to do it:
my_dict = {'1':['a','b','c'], '2':['d','e','f']}
with open('data.csv', 'w') as f:
f.write('\n'.join([("%s, %s" % (k,' '.join(my_dict[k]))) for k in my_dict])
my_dict = {'1':['a','b','c'], '2':['d','e','f']}
from operator import itemgetter
import csv
with open('data.csv','w') as f:
a = csv.writer(f, delimiter = ',', lineterminator='\n')
for k,v in sorted(my_dict.items(), key=itemgetter(0)):
a.writerow([k,' ' + ' '.join(v)])
data.csv
1, a b c
2, d e f
Hello I have a file comprising 4 columns,
a 1 45 test
b 2 42 test
c 3 64 test
I wish to read this file to a dictionary such that column 3 is the key and column 1 is the value, i.e.,
d = {45:'a', 42:'b', 64:'c'}
Keep it simple:
>>>
>>> d = dict()
>>> with open('test.txt') as f:
for line in f:
val, foo, key, bar = line.split()
d[key] = val
>>> d
{'64': 'c', '45': 'a', '42': 'b'}
>>>
Use the csv module to parse the file. Change the delimiter parameter to whatever is the delimiter in your input file. I have assumed it to be tabs.
import csv
d = {}
with open('your-input-file', 'r') as input_file:
csv_reader = csv.reader(input_file, delimiter='\t')
for row in csv_reader:
d[row[2]] = row[0]
input_file.close()
I have a dictionary with many, many key/value pairs.
The keys are dates and the values are worldwide top-level domains.
I want to output the dictionary to a text file so that it counts and alpha sorts similar values but only within the same key
for example:
*key: value1:count value2:count*
date1: au:4 be:12 com:44
date2: az:4 com:14 net:5
Code:
with open('access_logshort.txt','rU') as f:
for line in f:
list1 = re.search(r'(?P<Date>[0-9]{2}/[a-zA-Z]{3}/[0-9]{4})(.+)(GET|POST)\s(http://|https://)([a-zA-Z.]+)(\.)(?P<tld>[a-zA-Z]+)(/).+?"\s200',line)
if list1 != None:
print list1.groupdict()
one_tuple = list1.group(1,7)
my_dict[one_tuple[0]]=one_tuple[1]
output:
print my_dict
{'09/Mar/2004': 'hu'}
{'09/Mar/2004': 'hu'}
{'09/Mar/2004': 'com'}
{'09/Mar/2004': 'ru'}
{'09/Mar/2004': 'ru'}
{'09/Mar/2004': 'com'}
T
This should suit your case.
from collections import defaultdict
from dateutil.parser import parse
import csv
import re
data = defaultdict(lambda: defaultdict(int))
with open('access_logshort.txt','rU') as f:
for line in f:
list1 = re.search(r'(?P<Date>[0-9]{2}/[a-zA-Z]{3}/[0-9]{4})(.+)(GET|POST)\s(http://|https://)([a-zA-Z.]+)(\.)(?P<tld>[a-zA-Z]+)(/).+?"\s200',line)
if list1 is not None:
date, domain = list1.group(1,7)
data[date.lower()][domain.lower()] += 1
with open('my_data.csv', 'wb') as ofile:
# add delimiter='\t' to the argument list of csv.writer if you want
# tsv rather than csv
writer = csv.writer(ofile)
for key, value in sorted(data.iteritems(), key=lambda x: parse(x[0])):
domains = sorted(value.iteritems())
writer.writerow([key] + ['{}:{}'.format(*d) for d in domains])
Output:
10/Mar/2004,com:2,hu:2,ru:2
09/Mar/2004,com:2,hu:2,ru:2
I am trying to make a dictionary from a csv file in python, but I have multiple categories. I want the keys to be the ID numbers, and the values to be the name of the items. Here is the text file:
"ID#","name","quantity","price"
"1","hello kitty","4","9999"
"2","rilakkuma","3","999"
"3","keroppi","5","1000"
"4","korilakkuma","6","699"
and this is what I have so far:
txt = open("hk.txt","rU")
file_data = txt.read()
lst = [] #first make a list, and then convert it into a dictionary.
for key in file_data:
k = key.split(",")
lst.append((k[0],k[1]))
dic = dict(lst)
print(dic)
This just prints an empty list though. I want the keys to be the ID#, and then the values will be the names of the products. I will make another dictionary with the names as the keys and the ID#'s as the values, but I think it will be the same thing but the other way around.
Use the csv module to handle your data; it'll remove the quoting and handle the splitting:
results = {}
with open('hk.txt', 'r', newline='') as txt:
reader = csv.reader(txt)
next(reader, None) # skip the header line
for row in reader:
results[row[0]] = row[1]
For your sample input, this produces:
{'4': 'korilakkuma', '1': 'hello kitty', '3': 'keroppi', '2': 'rilakkuma'}
You can use csv DictReader:
import csv
result={}
with open('/tmp/test.csv', 'r', newline='') as f:
for d in csv.DictReader(f):
result[d['ID#']]=d['name']
print(result)
# {'1': 'hello kitty', '3': 'keroppi', '2': 'rilakkuma', '4': 'korilakkuma'}
You can use a dictionary directly:
dictionary = {}
file_data.readline() # skip the first line
for key in file_data:
key = key.replace('"', '').strip()
k = key.split(",")
dictionary[k[0]] = k[1]
try this or use any library to read the file.
txt = open("hk.txt","rU")
file_data = txt.read()
file_lines = file_data.split("\n")
lst = [] #first make a list, and then convert it into a dictionary.
for linenumber in range(1,len(file_lines)):
k = file_lines[linenumber].split(",")
lst.append((k[0][1:len(k[0])-1],k[1][1:len(k[1])-1]))
dic = dict(lst)
print(dic)
but you can use the dict directly as well.