Creating a Dictionary from a while loop (Python) - python-2.7

How can I create a Dictionary from my while loop below? infile reads from a file called input, which has the following content:
min:1,23,62,256
max:24,672,5,23
sum:22,14,2,3,89
P90:23,30,45.23
P70:12,23,24,57,32
infile = open("input.txt", "r")
answers = open("output.txt", "w")
while True:
line = infile.readline()
if not line: break
opType = line[0:3]
numList = (line[4:len(line)])
numList = numList.split(',')
What I'm trying to do is basically 2 lists, one that has the operation name (opType) and the other that has the numbers. From there I want to create a dictionary that looks like this
myDictionary = {
'min': 1,23,62,256,
'max': 24,672,5,23,
'avg': 22,14,2,3,89,
'P90': 23,30,45.23,
'P70': 12,23,24,57,32,
}
The reason for this is that I need to call the operation type to a self-made function, which will then carry out the operation. I'll figure this part out. I currently just need help making the dictionary from the while loop.
I'm using python 2.7

Try the following code.
I believe, you would need the 'sum' also in the dictionary. If not, just add a condition to remove it.
myDictionary = {}
with open('input.txt','r') as f:
for line in f:
x = line.split(':')[1].rstrip().split(',')
for i in xrange(len(x)):
try:
x[i] = int(x[i])
except ValueError:
x[i] = float(x[i])
myDictionary[line.split(':')[0]] = x
print myDictionary

Related

Declaring an object within (vs. outside) a while loop

Given the python 2.7 code below:
# faultReportObj = {}
flatDataObj = []
while row:
faultReportObj = {}
faultReportObj['MessageTime'] = row['MessageTime']
faultReportObj['Event'] = row['Event']
faultReportObj['Subsystem'] = row['Subsystem']
faultReportObj['UnifiedFaultCode'] = row['UnifiedFaultCode']
faultReportObj['FaultDescription'] = row['FaultDescription']
faultReportObj['FaultDetails'] = row['FaultDetails']
faultReportObj['FirstOccurrenceDateTime'] = row['FirstOccurrenceDateTime']
faultReportObj['LastOccurrenceDateTime'] = row['LastOccurrenceDateTime']
faultReportObj['OccurrenceCount'] = row['OccurrenceCount']
print "current row:"
pp.pprint(faultReportObj)
flatDataObj.append(faultReportObj)
row = cursor.fetchone()
conn.close()
pp.pprint(flatDataObj)
If I declare faultReportObj outside the while loop, I get (say) 96 entries in flatDataObj that are all identical to the very last row returned by the query. Note that the pprint statement within the while loop prints the expected (varying) results.
If, as above, I declare faultReportObj inside the loop, flatDataObj is loaded correctly.
Why???? Why is the very last row returned being propagated throughout the entire list?
Thanks!
This is due to list.append inserting a reference to faultReportObj and not copying the value of the dict.
Another way of looking at it:
If you define faultReportObj before the loop, the following occurs:
Create a new dict.
Populate it.
Append a reference to the dict into the list.
Change the dict's content.
Append another reference to the same dict.
etc.
Here's a short piece of code that exemplifies this property:
>>> d = {}
>>> l = []
>>> l.append(d)
>>> d[1] = 2
>>> l
[{1: 2}]
What you want is for step one (Create a new dict) to happen in every iteration of the loop, so that you append a different dict every time.

proper formatting printing the length of items in a dictionary

My code seems to be working, but I have having trouble with the print statement, which I will eventually write out to a CSV. I am able to get the print to work for the first two items, but when I try to add the len part as the third thing to print, it get an error "'str' object is not callable". When I print the len part by itself, it seems to work fine. Any insight as to what I am doing wrong to print all together?
inFile = open(file.txt,'r')
reader = csv.reader(inFile)
allrows = list(reader)
dd = defaultdict(OrderedDict)
ids = OrderedDict()
output = {}
iterallrows = iter(allrows)
next(iterallrows)
for row in iterallrows:
id_ = row[2]
name = row[3]
dd[id_][name] = None
ids[id_] = None
print('{} {} {}'.format(id_,','.join(dd[id_],','(len(dd[id_])))))
You have this:
[...],','(...)[...]
This attempts to treat ',' as a function, which it is not. Put a comma between all arguments to a function.

'list' object has no attribute 'items'

working on ubuntu 12.04, python 2.7
I have the error 'list' object has no attribute 'items' when I call the print_to_screen function.
could someone explain me please how to manage the dictionary which is created?
Thanks
class median_uniq:
def init(self):
self.median_number_list = []
def print_to_screen(self, words_dict, is_reverse = False):
words = words_dict.items()
words.sort(key = lambda(a,b):(a,b), reverse = is_reverse)
print("[Words tweeted: %d]" % len(words)).center(60,"=")
print("%-25s | %25s" % ("Words", "count"))
print BANNER
for w, c in words:
print("%-25s | %25d" % (w, c))
def Median_number(self, file_name):
file_object = open(file_name, "r")
number_word_list = []
for line in file_object:
unique_words_per_tweet = sorted(set(line.rstrip().split(" ")))
number_word_list.append(len(Counter((unique_words_per_tweet))))
self.median_number_list.append(numpy.median(numpy.array(number_word_list)))
print self.median_number_list
return self.median_number_list
to call function, I do this:
med = median_uniq() med_list = med.Median_number(input_file)
med.print_to_screen(med_list, is_reverse = False)
on command line knowing that my input_file is a .txt file. thanks
I have seen similar post, but don't well understand. An explanation with simply words would be really helpful.
Thanks
"words_dict" in your code is a list, but your attempt to use a python dictionary method (items())
I would need a working example to be able to propose a customized solution.

IndexError, but more likely I/O error

Unsure of why I am getting this error. I'm reading from a file called columns_unsorted.txt, then trying to write to columns_unsorted.txt. There error is on fan_on = string_j[1], saying list index out of range. Here's my code:
#!/usr/bin/python
import fileinput
import collections
# open document to record results into
j = open('./columns_unsorted.txt', 'r')
# note this is a file of rows of space-delimited date in the format <1384055277275353 0 0 0 1 0 0 0 0 22:47:57> on each row, the first term being unix times, the last human time, the middle binary indicating which machine event happened
# open document to read from
l = open('./columns_sorted.txt', 'w')
# CREATE ARRAY CALLED EVENTS
events = collections.deque()
i = 1
# FILL ARRAY WITH "FACTS" ROWS; SPLIT INTO FIELDS, CHANGE TYPES AS APPROPRIATE
for line in j: # columns_unsorted
line = line.rstrip('\n')
string_j = line.split(' ')
time = str(string_j[0])
fan_on = int(string_j[1])
fan_off = int(string_j[2])
heater_on = int(string_j[3])
heater_off = int(string_j[4])
space_on = int(string_j[5])
space_off = int(string_j[6])
pump_on = int(string_j[7])
pump_off = int(string_j[8])
event_time = str(string_j[9])
row = time, fan_on, fan_off, heater_on, heater_off, space_on, space_off, pump_on, pump_off, event_time
events.append(row)
You are missing the readlines function, no?
You have to do:
j = open('./columns_unsorted.txt', 'r')
l = j.readlines()
for line in l:
# what you want to do with each line
In the future, you should print some of your variables, just to be sure the code is working as you want it to, and to help you identifying problems.
(for example, if in your code you would print string_j you would see what kind of problem you have)
Problem was an inconsistent line in the data file. Forgive my haste in posting

Multidict in python not working ?? How to create it?

I want to create an python multi dimensional dictionary :-
Currently i am doing like this
multidict = {}
IN LOOP
mulitdict[i] = data
if loop runs ten times I am getting same value in all index..
Eg:
I want to have like this
multidict {0 : {'name':name1, 'age' : age1}, 1: {'name':name2, 'age' : age2}
but i am getting as shown below
multidict {0 : {'name':name1, 'age' : age1}, 1: {'name':name1, 'age' : age1}
I also tried the default dict also....but every time i get same value in all index. What is the problem?
Tried code :
csv_parsed_data2 = {}
with open('1112.txt') as infile:
i =0
for lineraw in infile:
line = lineraw.strip()
if 'sample1 ' in line:
### TO GET SOURCE ROUTER NAME ###
data['sample1'] = line[8:]
elif 'sample2- ' in line:
### TO GET DESTINATION ROUTER NAME ###
data['sample2'] = line[13:]
elif 'sample3' in line:
### TO GET MIN,MAX,MEAN AND STD VALUES ###
min_value = line.replace("ms"," ")
min_data = min_value.split(" ")
data['sample3'] = min_data[1]
csv_parsed_data2[i] = data
i = i + 1
print i,'::',csv_parsed_data2,'--------------'
print csv_parsed_data2,' all index has same value'
any efficient way to do this??
It sounds you are assigning the same data dict to each of the values of your outer multidict, and just modifying the values it holds on each pass through the loop. This will result in all the values appearing the same, with the values from the last pass through the loop.
You probably need to make sure that you create a separate dictionary object to hold the data from each value. A crude fix might be to replace multidict[i] = data with multidict[i] = dict(data), but if you know how data is created, you can probably do something more elegant.
Edit: Seeing your code, here's a way to fix the issue:
csv_parsed_data2 = {}
with open('1112.txt') as infile:
i =0
data = {} # start with empty data dict
for lineraw in infile:
line = lineraw.strip()
if 'sample1 ' in line:
### TO GET SOURCE ROUTER NAME ###
data['sample1'] = line[8:]
elif 'sample2- ' in line:
### TO GET DESTINATION ROUTER NAME ###
data['sample2'] = line[13:]
elif 'sample3' in line:
### TO GET MIN,MAX,MEAN AND STD VALUES ###
min_value = line.replace("ms"," ")
min_data = min_value.split(" ")
data['sample3'] = min_data[1]
csv_parsed_data2[i] = data
data = {} # after saving a reference to the dict, reinitialize it
i = i + 1
print i,'::',csv_parsed_data2,'--------------'
print csv_parsed_data2,' all index has same value'
To understand what was going on, consider this simpler situation, where I a values in a dictionary after saving a reference to it when it had some older values:
my_dict = { "foo": "bar" }
some_ref = my_dict
print some_ref["foo"] # prints "bar"
my_dict["foo"] = "baz"
print some_ref["foo"] # prints "baz", since my_dict and some_ref refer to the same object
print some_ref is d # prints "True", confirming that fact
In your code, my_dict was data and some_ref were all the values of csv_parsed_data2. They would all end up being references to the same object, which would hold whatever the last values assigned to data were.
Try this:
multidict = {}
for j in range(10):
s = {}
s['name'] = raw_input()
s['age'] = input()
multidict[j] = s
This will have the desired result