I have the following txt... (I've saved as a dictionary)
"{'03/01/20': ['luiana','macarena']}\n"
"{'03/01/21': ['juana','roberta','mariana']}\n"
"{'03/01/24': ['pedro','jose','mario','luis']}\n"
"{'03/01/24': ['pedro','jose','mario','luis']}\n"
"{'03/01/22': ['emanuel']}\n"
the problem is that I want to open it as a dictionary, but I don't know how I can do it. I've tried with:
f = open ('usuarios.txt','r')
lines=f.readlines()
whip=eval(str(lines))
but it's not working... my idea is for example just take the dictionaries that have as a value the next day 03/01/24
if you want to to have only one dict with all the saved dictionaries you can use:
import ast
my_dict = {}
with open('your_file.txt', 'r') as fp:
for line in fp.readlines():
new_dict = ast.literal_eval(line)
for key, value in new_dict.items():
if key in my_dict:
my_dict[key].extend(value)
else:
my_dict[key] = value
print(my_dict)
output:
{'03/01/20': ['luiana', 'macarena'], '03/01/21': ['juana', 'roberta', 'mariana'], '03/01/24': ['pedro', 'jose', 'mario', 'luis', 'pedro', 'jose', 'mario', 'luis'], '03/01/22': ['emanuel']}
if yo could change the format you are saving the strings from
"{'03/01/20': ['luiana','macarena']}\n"
to
'{"03/01/20": ["luiana","macarena"]}\n'
Then you could just do the following:
import json
line = '{"03/01/20": ["luiana","macarena"]}\n'
d = json.loads('{"03/01/20": ["luiana","macarena"]}\n')
The result would be a dictionary d with dates as keys:
>>> {'03/01/20': ['luiana', 'macarena']}
Them, it would be just a mater of looping over the lines of your file and adding them to your dictionary.
An alternative approach would be to use pickle to save your dictionary instead of the .txt, them use it to load from the disk.
Related
I have sample text file as below
asy1 10.20.0.1
byt 192.1.10.100
byt 192.1.10.101
byt 192.1.10.102
hps 10.30.1.50
hps 10.30.1.53
hps 10.30.1.54
hps 10.30.1.55
hps 10.30.1.56
zte 10.100.1.1
zte 10.100.1.2
When i run script below
mydict = {}
with open('devices.txt', 'r') as file:
for line in file:
name, ip = line.split()
mydict[name] = ip.strip()
print(mydict)
It not return all the line/content as per text file.
{'hps': '10.30.1.56', 'zte': '10.100.1.2', 'byt': '192.1.10.102', 'asy1': '10.20.0.1'}
I miss something here...Please advise me. Thanks
In a dictionary the key must be uniques, when you do:
mydict[name] = ip.strip()
you overwrite the value, instead of having a single value for a key you could store a list of values, by doing this:
mydict = {}
with open('devices.txt', 'r') as file:
for line in file:
name, ip = line.split()
if name not in mydict:
mydict[name] = []
mydict[name].append(ip.strip())
print(mydict)
Output
{'asy1': ['10.20.0.1'], 'byt': ['192.1.10.100', '192.1.10.101', '192.1.10.102'], 'hps': ['10.30.1.50', '10.30.1.53', '10.30.1.54', '10.30.1.55', '10.30.1.56'], 'zte': ['10.100.1.1', '10.100.1.2']}
A second alternative would be to use setdefault instead:
mydict.setdefault(name, []).append(ip.strip())
A third option would be to use a defaultdict. If the values are unique consider using a set.
Many of those names are the same, but a dict can only have one value for a given key - if you try to add an IP to the dictionary with key hps, but there's already one in there, it will be overwritten. Maybe use a list instead?
I had a tsv file like such
Name School Course
Nicole UVA Biology
Jenna GWU CS
from there,
I only want to print the Name and the Course from that dictionary. How would I go about this?
The code below is how I put the original TSV file into the dictionary above.
import csv
data = csv.reader(open('data.tsv'),delimiter='\t')
fields = data.next()
for row in data:
item = dict(zip(fields, row))
print item
So now I got a dictionary like such:
{'Name':'Nicole.', 'School':'UVA.','Course':'Biology'}
{'Name':'Jenna.', 'School':'GWU','Course':'CS'}
{'Name':'Shan', 'School':'Columbia','Course':'Astronomy'}
{'Name':'BILL', 'School':'UMD.','Course':'Algebra'}
I only want to print the Name and the Course from that dictionary. How would I go about this?
I want to add code so that I'm only printing
{'Name':'Jenna.','Course':'CS'}
{'Name':'Shan','Course':'Astronomy'}
{'Name':'BILL','Course':'Algebra'}
Please guide. Thank You
Just delete the key in the loop
for row in data:
item = dict(zip(fields, row))
del item['School']
print item
The easiest way is to just remove the item['School'] entry before printing
for row in data:
item = dict(zip(fields, row))
del item['School']
print item
But this only works if you know exactly what the dictionary looks like, it has no other entries that you don't want, and it already has a School entry. I would instead recommend that you build a new dictionary out of the old one, only keeping the Name and Course entries
for row in data:
item = dict(zip(fields, row))
item = {k, v for k, v in item.items() if k in ('Name', 'Course')}
print item
Maybe use a DictReader in the first place, and rebuild the row-dict only if key matches a pre-defined list:
import csv
keep = {"Name","Course"}
data = csv.DictReader(open('data.tsv'),delimiter='\t')
for row in data:
row = {k:v for k,v in row.items() if k in keep}
print(row)
result:
{'Course': 'Biology', 'Name': 'Nicole'}
{'Course': 'CS', 'Name': 'Jenna'}
Based on the answer here:
filter items in a python dictionary where keys contain a specific string
print {k:v for k,v in item.iteritems() if "Name" in k or "Course" in k}
You're better off using a library designed for these kinds of tasks (Pandas). A dictionary is great for storing key-value pairs, but it looks like you have spreadsheet-like tabular data, so you should choose a storage type that better reflects the data at hand. You could simply do the following:
import pandas as pd
df = pd.read_csv('myFile.csv', sep = '\t')
print df[['Name','Course']]
You'll find that as you start doing more complicated tasks, it's better to use well written libraries than to cludge something together
replace the your " print(item) " line with the below line.
print(dict(filter(lambda e: e[0]!='School', item)))
OUTPUT:
{'Name':'Jenna.','Course':'CS'}
{'Name':'Shan','Course':'Astronomy'}
{'Name':'BILL','Course':'Algebra'}
I have a file with 2 columns:
Anzegem Anzegem
Gijzelbrechtegem Anzegem
Ingooigem Anzegem
Aalst Sint-Truiden
Aalter Aalter
The first column is a town and the second column is the district of that town.
I made a dictionary of that file like this:
def readTowns(text):
input = open(text, 'r')
file = input.readlines()
dict = {}
verzameling = set()
for line in file:
tmp = line.split()
dict[tmp[0]] = tmp[1]
return dict
If I set a variable 'writeTowns' equal to readTowns(text) and do writeTown['Anzegem'], I want to get a collection of {'Anzegem', 'Gijzelbrechtegem', 'Ingooigem'}.
Does anybody know how to do this?
I think you can just create another function that can create appropriate data structure for what you need. Because, at the end you will end up writing code which basically manipulates the dictionary returned by readTowns to generate data as per your requirement. Why not keep the code clean and create another function for that. You Just create a name to list dictionary and you are all set.
def writeTowns(text):
input = open(text, 'r')
file = input.readlines()
dict = {}
for line in file:
tmp = line.split()
dict[tmp[1]] = dict.get(tmp[1]) or []
dict.get(tmp[1]).append(tmp[0])
return dict
writeTown = writeTowns('file.txt')
print writeTown['Anzegem']
And if you are concerned about reading the same file twice, you can do something like this as well,
def readTowns(text):
input = open(text, 'r')
file = input.readlines()
dict2town = {}
town2dict = {}
for line in file:
tmp = line.split()
dict2town[tmp[0]] = tmp[1]
town2dict[tmp[1]] = town2dict.get(tmp[1]) or []
town2dict.get(tmp[1]).append(tmp[0])
return dict2town, town2dict
dict2town, town2dict = readTowns('file.txt')
print town2dict['Anzegem']
You could do something like this, although, please have a look at #ubadub's answer, there are better ways to organise your data.
[town for town, region in dic.items() if region == 'Anzegem']
It sounds like you want to make a dictionary where the keys are the districts and the values are a list of towns.
A basic way to do this is:
def readTowns(text):
with open(text, 'r') as f:
file = input.readlines()
my_dict = {}
for line in file:
tmp = line.split()
if tmp[1] in dict:
my_dict[tmp[1]].append(tmp[0])
else:
my_dict[tmp[1]] = [tmp[0]]
return dict
The if/else blocks can also be achieved using python's defaultdict subclass (docs here) but I've used the if/else statements here for readability.
Also some other points: the variables dict and file are python types so it is bad practice to overwrite these with your own local variable (notice I've changed dict to my_dict in the code above.
If you build your dictionary as {town: district}, so the town is the key and the district is the value, you can't do this easily*, because a dictionary is not meant to be used in that way. Dictionaries allow you to easily find the values associated with a given key. So if you want to find all the towns in a district, you are better of building your dictionary as:
{district: [list_of_towns]}
So for example the district Anzegem would appear as {'Anzegem': ['Anzegem', 'Gijzelbrechtegem', 'Ingooigem']}
And of course the value is your collection.
*you could probably do it by iterating through the entire dict and checking where your matches occur, but this isn't very efficient.
I have a Json file with a dictionary that looks like
{"tvs": 92, "sofas": 31, "chairs": 27, "cpus": 007}
I'm trying loop though the dictionary and print the key with its corresponding value, in my code I'm getting a too many values to unpack error.
with open('myfile.json', "r") as myfile:
json_data = json.load(myfile)
for e, v in json_data:
for key, value in e.iteritem():
print key, value
So, by default a dict will iterate over its keys.
for key in json_data:
print key
# tvs, sofas, etc...
Instead, it seems like you want to iterate over the key-value pairs. This can be done by calling .items() on the dictionary.
for key, value in json_data.items():
print key, value
Or you can iterate over just the values by calling .values().
Try this:
with open('myfile.json', "r") as myfile:
json_data = json.load(myfile)
for e, v in json_data.items():
print e,v
You have an extra loop in your code, also, the input file has invalid data 007. Loading it into json should give you an error.
Is this what you're looking for?
>>> json = {"tvs": 92, "sofas": 31, "chairs": 27, "cpus": 007}
>>> for j in json:
... print j, json[j]
...
chairs 27
sofas 31
cpus 7
tvs 92
These are the contents of my text file (eg:abc.doc):
{'data': [{'name': 'abc'},{'name': 'xyz'}]}
After opening the file in python; how do i remove all the brackets, quotes and commas.
The final output should be:
data:
name:abc
name:xyz
Use ast.literal_eval() to turn it into a python structure, then print the values:
with open(r'd:\output1.doc', 'r') as inputfile:
inputstring = inputfile.read()
data = ast.literal_eval(inputstring)
for key, sublist in data.items():
print '{}:'.format(key)
for subdict in sublist:
for key, value in subdict.items():
print('{}:{}'.format(key, value))
For your example that results in:
>>> inputstring = "{'data': [{'name': 'abc'},{'name': 'xyz'}]}"
>>> import ast
>>> data = ast.literal_eval(inputstring)
>>> for key, sublist in data.items():
... print '{}:'.format(key)
... for subdict in sublist:
... for key, value in subdict.items():
... print '{}:{}'.format(key, value)
...
data:
name:abc
name:xyz
However: If you got this from the Facebook API, then you transcribed the format incorrectly. The Facebook API gives you JSON data, which uses double quotes (") instead:
{"data": [{"name": "abc"},{"name": "xyz"}]}
in which case you should use the json library that comes with Python:
import json
data = json.loads(inputstring)
# process the same way as above.
If you have a filename, you can ask the library to read straight from the file using:
data = json.load(filename) # note, no `s` after `load`.
Looks to me like you have json, which can be easily parsed using pyjson:
import json
obj=json.loads(u'''{'data': [{'name': 'abc'},{'name': 'xyz'}]}''')
Bob's your uncle now, innit?