So I have a .txt file and I want to use methods within this class, Map, to append its contents into a aDictionary.
class Map:
def __init__(self, dataText):
self.dataText = dataText
self.aDictionary = {}
dataFile = open('data.txt', 'r')
c1 = Map(dataFile)
My data.txt file looks something like this:
hello, world
how, are
you, today
and I want aDictionary to print this output:
{how: are, you: today}
Im not very good at manipulating files as I continue to get type errors and what not. Is there an easy way of performing this task using methods within the class?
First you need to read the content of the file. Once you have the content of the file, you could create the dictionary like this (assuming content contains the content of data.txt):
content = """hello, world
how, are
you, today"""
d = {}
for line in content.splitlines():
if line:
key, value = map(str.strip, line.split(','))
d[key] = value
print(d)
Output
{'you': 'today', 'how': 'are', 'hello': 'world'}
The idea is to iterate of over the lines using a for loop, then check if the line is not empty (if line), in case the line is not empty, split on comma (line.split(',')) and remove the trailing whitespaces (str.strip) for each of the values in the list using map.
Or using a dictionary comprehension:
content = """hello, world
how, are
you, today"""
it = (map(str.strip, line.split(',')) for line in content.splitlines() if line)
d = {key: value for key, value in it}
print(d)
To read the content of the file you can do the following:
content = self.dataText.read()
Further
Reading entire file in Python
How to read a file line-by-line into a list?
Related
I'm very new to Python and I'm having trouble working on an assignment which basically is like this:
#Read line by line a WARC file to identify string1.
#When string1 found, add part of the string as a key to a dictionary.
#Then continue reading file to identify string2, and add part of string2 as a value to the previous key.
#Keep going through file and doing the same to build the dictionary.
I can't import anything so it's causing me a bit of trouble, especially adding the key, then leaving the value empty and continue going through the file to find string2 to be used as value.
I've started thinking something like saving the key to an intermediate variable, then going on to identify the value, add to an intermediate variable and finally build the dictionary.
def main ():
###open the file
file = open("warc_file.warc", "rb")
filetxt = file.read().decode('ascii','ignore')
filedata = filetxt.split("\r\n")
dictionary = dict()
while line in filedata:
for line in filedata:
if "WARC-Type: response" in line:
break
for line in filedata:
if "WARC-Target-URI: " in line:
urlkey = line.strip("WARC-Target-URI: ")
It's not entirely clear what you're trying to do, but I'll have a go at answering.
Suppose you have a WARC file like this:
WARC-Type: response
WARC-Target-URI: http://example.example
something
WARC-IP-Address: 88.88.88.88
WARC-Type: response
WARC-Target-URI: http://example2.example2
something else
WARC-IP-Address: 99.99.99.99
Then you could create a dictionary that maps the target URIs to the IP addresses like this:
dictionary = dict()
with open("warc_file.warc", "rb") as file:
urlkey = None
value = None
for line in file:
if b"WARC-Target-URI: " in line:
assert urlkey is None
urlkey = line.strip(b"WARC-Target-URI: ").rstrip(b"\n").decode("ascii")
if b"WARC-IP-Address: " in line:
assert urlkey is not None
assert value is None
value = line.strip(b"WARC-IP-Address: ").rstrip(b"\n").decode("ascii")
dictionary[urlkey] = value
urlkey = None
value = None
print(dictionary)
This prints the following result:
{'http://example.example': '88.88.88.88', 'http://example2.example2': '99.99.99.99'}
Note that this approach only loads one line of the file into memory at a time, which might be significant if the file is very large.
Your idea with storing the key to an intermediate value is good.
I also suggest using the following snippet to iterate over the lines.
with open(filename, "rb") as file:
lines = file.readlines()
for line in lines:
print(line)
To create dictionary entries in Python, the dict.update() method can be used.
It allows you to create new keys or update values if the key already exists.
d = dict() # create empty dict
d.update({"key" : None}) # create entry without value
d.update({"key" : 123}) # update the value
I have a file syntaxed in a way that ressembles a Dictionary as follows:
{YEARS:5}
{GROUPS:[1,2]}
{SAVE_FILE:{USE:1,NAME:CustomCalendar.ics}}
{SAVE_ONLINE:{USE:1,NAME:Custom Calendar,EMAIL:an.email#something.com,PASSWORD:AcompLExP#ssw0rd}}
{COURSES:[BTC,CIT,CQN,OSA,PRJCQN,PRJIOT,PILS,SPO,SHS1]}
I would like to find a way to parse each individual line into a dictionary as it is written. The difficulty I have is that some of these lines contain a dictionary as their value.
I am capable of taking the single lines and converting them to actual dictionaries but I am having an issue when working with the other lines.
Here is the code I have so far:
def get_config(filename=):
with open(filename, encoding="utf8") as config:
years = config.read().split()[0]
print(parse_line(years))
def parse_line(input_line):
input_line = input_line.strip("{}")
input_line = input_line.split(":")
return {input_line[i]: input_line[i + 1] for i in range(0, len(input_line), 2)}
If at all possible, I'd love to be able to deal with any line within a single function and hopefully deal with more than two nested dictionaries.
Thanks in advance!
If your file would contain valid JSON format, it would be an easy task to read the file and convert your data structures to dictionaries.
To give an example, consider having the following line of text in a file text.txt:
{"SAVE_ONLINE":{"USE":1,"NAME":"Custom Calendar","EMAIL":"an.email#something.com","PASSWORD":"AcompLExP#ssw0rd"}}
Please note, that the only difference are the quotes " around strings.
You can easily parse the line to a dictionary structure with:
import json
with open('text.txt', 'r') as f:
d = json.loads(f.read())
Output
print(d)
# {'SAVE_ONLINE': {'USE': 1, 'NAME': 'Custom Calendar', 'EMAIL': 'an.email#something.com', 'PASSWORD': 'AcompLExP#ssw0rd'}}
I'm still quite new to Python and I was wondering how would I convert something that is already in key:value form in a text file into a Python dictionary?
Eg.
2:red
3:orange
5:yellow
6:green
(each key:value on a separate line)
I've looked at other posts but none of them seem to work and I know I'm doing something wrong. So far, I have:
def create_colours_dictionary(filename):
colours_dict = {}
file = open(filename,'r')
contents = file.read()
for key in contents:
#???
return colours_dict
The straight-forward way to do this is to use a traditional for loop, and the str.split method.
Rather than reading from a file, I'll embed the input data into the script as a multi-line string, and use str.splitlines to convert it to a list of strings, so we can loop over it, just like looping over the lines of a file.
# Use a list of strings to simulate the file
contents = '''\
2:red
3:orange
5:yellow
6:green
'''.splitlines()
colours_dict = {}
for s in contents:
k, v = s.split(':')
colours_dict[k] = v
print(colours_dict)
output
{'2': 'red', '3': 'orange', '5': 'yellow', '6': 'green'}
Be aware that this code will only work correctly if there are no spaces surrounding the colon. If there could be spaces (or spaces at the start or end of the line), they you can use the str.strip method to remove them.
There are a couple of ways to make this more compact.
We could use a list comprehension nested inside a dictionary comprehension:
colours_dict = {k: v for k, v in [s.split(':') for s in contents]}
But it's even more compact to use the dict constructor on a generator expression:
colours_dict = dict(s.split(':') for s in contents)
If you aren't familiar with comprehensions, please see
List Comprehensions and Dictionaries in the official tutorial.
Iterate over your file and build a dictionary.
def create_colours_dictionary(filename):
colours_dict = {}
with open(filename) as file:
for line in file:
k, v = line.rstrip().split(':')
colours_dict[k] = v
return colours_dict
dct = create_colours_dictionary('file.txt')
Or, if you're looking for something compact, you can use a dict comprehension with a lambda to split on colons.
colours_dict = {k : v for k, v in (
line.rstrip().split(':') for line in open(filename)
}
This approach will need some modification if the colon is surrounded by spaces—perhaps regex?
Assuming the textfile has the stated 'key:value' and the name of the file is contained in the variable fname you could write a function that will read the file and return a dict or just use a simple with statment.
A function is probably a better choice if this opertion is performed in several places in your code. If only done once a 2-liner will do fine.
# Example with fname being the path to the textfile
def dict_from(fname):
return dict(line.strip().split(':') for line in open(fname))
fname = '...'
# ...
d1 = dict_from(fname)
# Alternative solution
with open(fname) as fd:
d2 = dict(line.strip().split(':') for line in fd)
Both suggested solutions uses a built-in dictconstructor and a generator expression to parse each line. Use strip to remove white space at both start and end of the line. Use split create a (key, value) pair from each line.
I have a text file (one.txt) that contains an arbitrary number of key‐value pairs (where the key and value are separated by a colon – e.g., x:17). Here are some (minus the numbers):
mattis:turpis
Aliquam:adipiscing
nonummy:ligula
Duis:ultricies
nonummy:pretium
urna:dolor
odio:mauris
lectus:per
quam:ridiculus
tellus:nonummy
consequat:metus
I need to open the file and create a dictionary that contains all of the key‐value pairs.
So far I have opened the file with
file = []
with open('one.txt', 'r') as _:
for line in _:
line = line.strip()
if line:
file.append(line)
I opened it this way to get rid of new line characters and the last black line in the text file. I am given a list of the key-value pairs within python.
I am not sure how to create a dictionary with the list key-value pairs.
Everything I have tried gives me an error. Some say something along the lines of
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Use str.split():
with open('one.txt') as f:
d = dict(l.strip().split(':') for l in f)
split() will allow you to specify the separator : to separate the key and value into separate strings. Then you can use them to populate a dictionary, for example: mydict
mydict = {}
with open('one.txt', 'r') as _:
for line in _:
line = line.strip()
if line:
key, value = line.split(':')
mydict[key] = value
print mydict
output:
{'mattis': 'turpis', 'lectus': 'per', 'tellus': 'nonummy', 'quam': 'ridiculus', 'Duis': 'ultricies', 'consequat': 'metus', 'nonummy': 'pretium', 'odio': 'mauris', 'urna': 'dolor', 'Aliquam': 'adipiscing'}
I have a dictionary that searches for an ID name and reads tokens after it. But I want to know if there is a way to read and print out the whole line that contains that ID name as well.
Here is what I have so far:
lookup = defaultdict(list)
wholelookup =defaultdict(list)
mydata = open('summaryfile.txt')
for line in csv.reader(mydata, delimiter='\t'):
code = re.match('[a-z](\d+)[a-z]', line[-1], re.I)
if code:
lookup[line[-2]].append(code.group(1))
wholelookup[line[-2]].append(code.group(0))
Your code calls csv.reader() which will return a parsed version of the whole line. In my test, this returns a list of values. If this list of values will do for the "whole line" then you can save that.
You have a line where you append something called wholelookup. I think you want to just save line there instead of code.group(0). code.group(0) returns everything matched by the regular expression, and this would be identical to line[-1].
So maybe put this line in your code:
wholelookup[line[-2]].append(line)
Or maybe you need to join together the values from line to make a single string:
s = ' '.join(line)
wholelookup[line[-2]].append(s)
If you want the whole line, not the parsed version, then do something like this:
lookup = defaultdict(list)
wholelookup = defaultdict(list)
pat = re.compile('[a-z](\d+)[a-z]', re.I)
with open('summaryfile.txt') as mydata:
for s_line in mydata:
values = s_line.split('\t')
code = re.match(pat, values[-1])
if code:
lookup[values[-2]].append(code.group(1))
wholelookup[values[-2]].append(s_line)
This example pre-compiles the pattern for the slight speed advantage.
If you have enough memory, the easiest way is to simply save the lines in another defaultdict:
wholeline = defaultdict(list)
...
idname = line[-2]
wholeline[idname].append(line)