Python 2.7 Unicode Dict - python
I'm wondering how I can encode Dicts in Python 2.7. When I do the following:
# coding=ISO-8859-1
Programmer = {'v': ["1","2x","3","4","5"]}
Programmer.update({'x': ["1","2x","3","4","5"]})
Programmer.update({'y': ["1","2x","3","4","5"]})
Programmer.update({'z': ["1","2x","3","4","5"]})
Programmer.update({'æ': ["1","2x","3","4","5"]})
Programmer.update({'ø': ["1","2x","3","4","5"]})
Programmer.update({'å': ["1","2x","3","4","5"]})
print Programmer
I get:
{'v': ['1', '2x', '3', '4', '5'], '\xe5': ['1', '2x', '3', '4', '5'], '\xf8': ['1', '2x', '3', '4', '5'], '\xe6': ['1', '2x', '3', '4', '5'], 'y': ['1', '2x', '3', '4', '5'], 'x': ['1', '2x', '3', '4', '5'], 'z': ['1', '2x', '3', '4', '5']}
And what I want is:
{'v': ['1', '2x', '3', '4', '5'], 'å': ['1', '2x', '3', '4', '5'], 'ø': ['1', '2x', '3', '4', '5'], 'æ': ['1', '2x', '3', '4', '5'], 'y': ['1', '2x', '3', '4', '5'], 'x': ['1', '2x', '3', '4', '5'], 'z': ['1', '2x', '3', '4', '5']}
EDIT:
This is my working code:
# -*- coding: cp1252 -*-
from PyQt4 import QtCore, QtGui
import locale
global Programmer
Programmer = {u'v': ["1","2x","3","4","5"]}
Programmer.update({u'x': ["1","2x","3","4","5"]})
Programmer.update({u'y': ["1","2x","3","4","5"]})
Programmer.update({u'z': ["1","2x","3","4","5"]})
Programmer.update({u'æ': ["1","2x","3","4","5"]})
Programmer.update({u'ø': ["1","2x","3","4","5"]})
Programmer.update({u'å': ["1","2x","3","4","5"]})
class Dialog(QtGui.QDialog):
NumGridRows = 3
NumButtons = 3
def __init__(self):
super(Dialog, self).__init__()
styleComboBox = QtGui.QComboBox()
styleComboBox.addItem("UI Item01")
styleLabel = QtGui.QLabel("&UI Text:")
styleLabel.setBuddy(styleComboBox)
opgaveComboBox = QtGui.QComboBox()
opgaveComboBox.addItem(u"v")
opgaveComboBox.addItem(u"x")
opgaveComboBox.addItem(u"å")
opgaveLabel = QtGui.QLabel("&Opgave:")
opgaveLabel.setBuddy(opgaveComboBox)
self.programComboBox = QtGui.QComboBox()
self.programComboBox.addItem("UI item02")
programLabel = QtGui.QLabel("&Program:")
programLabel.setBuddy(self.programComboBox)
opgaveComboBox.activated[str].connect(self.changeOpgave)
topLayout = QtGui.QHBoxLayout()
topLayout.addWidget(styleLabel)
topLayout.addWidget(styleComboBox)
#topLayout.addStretch(1)
topLayoutB = QtGui.QHBoxLayout()
topLayoutB.addWidget(opgaveLabel)
topLayoutB.addWidget(opgaveComboBox)
#topLayoutB.addStretch(1)
topLayoutC = QtGui.QHBoxLayout()
topLayoutC.addWidget(programLabel)
topLayoutC.addWidget(self.programComboBox)
mainLayout = QtGui.QGridLayout()
mainLayout.addLayout(topLayout, 0, 0, 1, 2)
mainLayout.addLayout(topLayoutB, 1, 0, 1, 2)
mainLayout.addLayout(topLayoutC, 2, 0, 1, 2)
mainLayout.setRowStretch(1, 1)
mainLayout.setRowStretch(2, 1)
mainLayout.setColumnStretch(0, 1)
mainLayout.setColumnStretch(1, 1)
self.setLayout(mainLayout)
self.changeStyle('Cleanlooks')
self.setWindowTitle("temp test")
def changeOpgave(self, opgaveName):
self.programComboBox.clear()
print "begin 01"
print Programmer[opgaveName]
if not opgaveName in Programmer:
print "begin 02"
self.programComboBox.addItem("Andet")
else:
print "go"
for item in Programmer[opgaveName]:
self.programComboBox.addItem(item)
def changeStyle(self, styleName):
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(styleName))
self.changePalette()
def changePalette(self):
QtGui.QApplication.setPalette(QtGui.QApplication.style().standardPalette())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
dialog = Dialog()
dialog.show()
sys.exit(dialog.exec_())
I get
KeyError: PyQt4.QtCore.QString(u'v')
KeyError: PyQt4.QtCore.QString(u'x')
KeyError: "PyQt4.QtCore.QString(u'\xe5')
When trying to select from the "opgave" list
(error thrown at line 79)
The combobox callback is handed a PyQt4.QtCore.QString() object, not a unicode string. Your dictionary on the other hand should use unicode() keys (which is the right thing to do).
Convert the QString() object to a unicode value first:
def changeOpgave(self, opgaveName):
opgaveName = unicode(opgaveName)
This works because the QString() object already holds unicode data and provides Python with the right hooks to make this conversion without needing to re-encode the data with an explicit codec.
Once the opgaveName value is a unicode key, you can use it to compare it against your dictionary.
Note that you can just define the dictionary without .update() calls:
Programmer = {
u'v': ["1","2x","3","4","5"],
u'x': ["1","2x","3","4","5"],
u'y': ["1","2x","3","4","5"],
u'z': ["1","2x","3","4","5"],
u'æ': ["1","2x","3","4","5"],
u'ø': ["1","2x","3","4","5"],
u'å': ["1","2x","3","4","5"],
}
The global keyword has no meaning where you are using it; that keyword only makes sense inside functions, and is redundant here.
may be like this:
print "{",
for key, value in Programmer.items():
print "'%s'" % key, ':', value, ',',
print "}"
when you do print then this will print str(Programmers) but inside dict uses repr(key) and repr(value). The content does not change, it will just be printed differently.
Related
String Replace turn result to integers python
line = ['R2', 'R5', 'R6', 'R7', 'R10', 'R12', 'R13', 'R15', 'R18', 'R20', 'R21', 'R22', 'R23', 'R24', 'R26', 'R30', 'R31', 'R32', 'R33', 'R34', 'R35', 'R36', 'R37', 'R38', 'R44', 'R45', 'R46', 'R47', 'R48', 'R53', 'R55', 'R56', 'R63', 'R66', 'R68', 'R69', 'R70', 'R74', 'R78', 'R80', 'R81', 'R82', 'R83', 'R84', 'R90', 'R91', 'R92', 'R97', 'R98', 'R99', 'R100', 'R101', 'R102', 'R103', 'R108', 'R109', 'R111', 'R115', 'R116', 'R117', 'R118', 'R120', 'R121', 'R123', 'R124', 'R126', 'R130', 'R131', 'R132', 'R136', 'R138', 'R141', 'R142', 'R151', 'R152', 'R153', 'R154', 'R155', 'R156', 'R158', 'R160', 'R161', 'R163', 'R164', 'R167', 'R169', 'R171', 'R172', 'R177', 'R178', 'R179', 'R180', 'R181', 'R182', 'R183', 'R184', 'R186', 'R190', 'R192', 'R195', 'R196', 'R199', 'R201', 'R203', 'R204', 'R205', 'R206', 'R207', 'R209', 'R210', 'R211', 'R213', 'R214', 'R215', 'R216', 'R217', 'R223', 'R225', 'R227', 'R228', 'R229', 'R231', 'R233', 'R237', 'R238', 'R239', 'R240', 'R245', 'R246', 'R247', 'R248', 'R249', 'R250', 'R252', 'R253', 'R254', 'R255', 'R257', 'R258', 'R265', 'R266', 'R267', 'R268', 'R270', 'R271', 'R273', 'R275', 'R276', 'R277', 'R279', 'R280', 'R281', 'R284', 'R287', 'R290', 'R291', 'R292', 'R293', 'R294', 'R296', 'R299', 'R300', 'R301', 'R303', 'R304', 'R305', 'R306', 'R307', 'R308', 'R309', 'R310', 'R312', 'R313', 'R314', 'R315', 'R317', 'R320', 'R321', 'R323', 'R325', 'R327', 'R329', 'R330', 'R331', 'R334', 'R335', 'R336', 'R339', 'R340', 'R341', 'R343', 'R344', 'R345', 'R347', 'R349', 'R350', 'R352', 'R354', 'R356', 'R358', 'R359', 'R360', 'R361', 'R362', 'R363', 'R365', 'R367', 'R372', 'R373', 'R376', 'R378', 'R379', 'R381', 'R384', 'R386', 'R388', 'R393', 'R397', 'R398', 'R399', 'R401', 'R402', 'R403', 'R405', 'R406', 'R407', 'R410', 'R411', 'R413', 'R414', 'R416', 'R419', 'R420', 'R421', 'R422', 'R427', 'R430', 'R433', 'R434', 'R435', 'R438', 'R439', 'R443', 'R444', 'R445', 'R453', 'R454', 'R455', 'R456', 'R458', 'R459', 'R460', 'R461', 'R462', 'R463', 'R464', 'R466', 'R467', 'R468', 'R469', 'R470', 'R473', 'R475', 'R476', 'R478', 'R481', 'R482', 'R483', 'R485', 'R487', 'R488', 'R489', 'R490', 'R492', 'R494', 'R496', 'R497', 'R499', 'R500', 'R502', 'R503', 'R506', 'R508', 'R509', 'R511', 'R512', 'R516', 'R517', 'R519', 'R520', 'R528', 'R530', 'R532', 'R537', 'R539', 'R540', 'R541', 'R542', 'R544', 'R545', 'R547', 'R549', 'R550', 'R552', 'R555', 'R558', 'R560', 'R561', 'R562', 'R563', 'R564', 'R565', 'R566', 'R568', 'R571', 'R572', 'R573', 'R574', 'R576', 'R579', 'R581', 'R583', 'R584', 'R585', 'R586', 'R588', 'R590', 'R592', 'R593', 'R595', 'R597', 'R598', 'R600', 'R601', 'R606', 'R607', 'R609', 'R610', 'R612', 'R613', 'R614', 'R615', 'R617', 'R618', 'R619', 'R620', 'R622', 'R624', 'R625', 'R626', 'R627', 'R628', 'R629', 'R630', 'R631', 'R632', 'R633', 'R634', 'R635', 'R636', 'R638', 'R642', 'R644', 'R645', 'R646', 'R647', 'R648', 'R649', 'R650', 'R652', 'R654', 'R657', 'R658', 'R660', 'R662', 'R668', 'R669', 'R671', 'R672', 'R673', 'R674', 'R676', 'R677', 'R678', 'R683', 'R684', 'R686', 'R689', 'R691', 'R693', 'R694', 'R696', 'R697', 'R698', 'R700', 'R701', 'R704', 'R706', 'R707', 'R710', 'R712', 'R713', 'R715', 'R716', 'R718', 'R719', 'R721', 'R724', 'R725', 'R728', 'R729', 'R730', 'R731', 'R733', 'R734', 'R737', 'R738', 'R739', 'R740', 'R743', 'R744', 'R745', 'R747', 'R748', 'R751', 'R755', 'R756', 'R757', 'R758', 'R759', 'R761', 'R762', 'R763', 'R767', 'R768', 'R770', 'R773', 'R774', 'R775', 'R776', 'R777', 'R779', 'R783', 'R785', 'R786', 'R789', 'R791', 'R795', 'R796', 'R797', 'R798', 'R800', 'R801', 'R802', 'R804', 'R805', 'R806', 'R808', 'R818', 'R819', 'R820', 'R822', 'R824', 'R826', 'R827', 'R829', 'R830', 'R831', 'R833', 'R835', 'R836', 'R838', 'R840', 'R841', 'R843', 'R844', 'R845', 'R847', 'R848', 'R849', 'R850', 'R851', 'R852', 'R853', 'R855', 'R858', 'R859', 'R860', 'R862', 'R863', 'R865', 'R867', 'R868', 'R869', 'R870', 'R874', 'R875', 'R876', 'R880', 'R881', 'R882', 'R884', 'R885', 'R886', 'R889', 'R891', 'R892', 'R893', 'R894', 'R895', 'R897', 'R900', 'R906', 'R908', 'R909', 'R911', 'R912', 'R914', 'R917', 'R918', 'R921', 'R922', 'R924', 'R925', 'R926', 'R927', 'R928', 'R929', 'R930', 'R932', 'R935', 'R937', 'R943', 'R944', 'R945', 'R946', 'R947', 'R948', 'R949', 'R951', 'R952', 'R953', 'R956', 'R957', 'R958', 'R959', 'R961', 'R964', 'R965', 'R966', 'R967', 'R968', 'R969', 'R971', 'R972', 'R974', 'R975', 'R976', 'R979', 'R981', 'R982', 'R983', 'R986', 'R987', 'R988', 'R990', 'R993', 'R994', 'R997'] ref = str(line).replace("R","") print(ref) it prints ('2', '5', '6', '7', '10', '12', '13', '15', '18', '20', '21', '22', '23', '24', '26', '30', '31', '32', '33', ...... and so on Would like to turn to integers(2,5,6,7,10,12,13,15,18... and so on. I have tried but no good results.
Use list comprehension line = [int(l.replace("R", "")) for l in line] If "R" is always the first character, you can also do: line = [int(l[1:]) for l in line] Per the data provided, both solution above will work solve the original question, if you may have invalid data, for example R32R the above will fail and raise a ValueError exception. To prevent this you can discard any value that are not an integer by using: item.isnumeric() line = [int(l[1:]) for l in line if l[1:].isnumeric()] You can also achieve the same with a regex expression re.search('\d+', line[0]).group(0) line = [int(re.search('\d+', l).group(0)) for l in line if re.search('\d+', l) is not None] You will need the following import: import re
How to save the list in to .txt?
I have to save 'Record' into text file. So I have tried to use name_error_record.write( record[0]+" "+record[1]+" "+record[2]+" "+record[3]) but it doesn't work... I have to align as 'No Menu_name Num Total' and save it into text file. please help.. The list that I have to save is [['No.', 'Menu_name', 'Num', 'Total'], ['4', 'Smoothie_queen', '4', '12000'], ['10', 'Amorparty', '2', '4000'], ['24', 'Plane_yougurt', '14', '49000'], ['36', 'Cofe_latte', '18', '45000'], ['51', 'Cofe_latte', '16', '40000'], ['53', 'Berry_smudie', '17', '51000'], ['54', 'American_air', '4', '8000']] and also it has to be aligned by columns as 'No Menu name Num Total'. I think I am missing something but I can't find them.. help T.T def error_check(date): #========= STEP 3 ========== Record = [] errormenu = [] recordfile = open("ledger_"+date+".txt","r") errormenufile = open("menu.txt", "r") for error in errormenufile: menu = error.split() errormenu.append(menu[0]) errormenufile.close() for line in recordfile: record = line.split() Record.append(record) Record = [x for x in Record if errormenu[0] not in x] Record = [x for x in Record if errormenu[1] not in x] Record = [x for x in Record if errormenu[2] not in x] Record = [x for x in Record if errormenu[3] not in x] Record = [x for x in Record if errormenu[4] not in x] name_error_record = open("ledger_"+date+"_name_error.txt","r") for record in Record: name_error_record.write( record[0]+" "+record[1]+" "+record[2]+" "+record[3]) name_error_record.close() #========= STEP 3 ==========
Simply change 'r' to 'w' in: open("ledger_"+date+"_name_error.txt","r") open("ledger_"+date+"_name_error.txt","w") by setting 'w' you open file for writing you can add '\n' at the end of saved line in: name_error_record.write( record[0]+" "+record[1]+" "+record[2]+" "+record[3]+'\n') to save data line by line
l=[['No.', 'Menu_name', 'Num', 'Total'], ['4', 'Smoothie_queen', '4', '12000'], ['10', 'Amorparty', '2', '4000'], ['24', 'Plane_yougurt', '14', '49000'], ['36', 'Cofe_latte', '18', '45000'], ['51', 'Cofe_latte', '16', '40000'], ['53', 'Berry_smudie', '17', '51000'], ['54', 'American_air', '4', '8000']] filename='' # add filenmae with open(filename, 'w+') as file: for i in l: file.write('{} {} {} {}\n'.format(*i))
For loop vs. class-object... How can I solve it?
today I tried to create a class. I wrote this. from first_names_class import firstnames from last_names_class1 import lastnames from object_name_list import my_new_list class MyClass: """A Class with automatated full names.""" # Automatically create the instances of the class named user_indexfrom the rangeiterator def __init__(self, first_name, last_name): self.first_name = first_name self.last_name = last_name for i in range(0, 49): my_new_list[i] = MyClass(firstnames[i], lastnames[i]) print(user1.first_name) However, when I run it it says: Traceback (most recent call last): ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50'] File "/Users/myname/Documents/University/Programming for Quantitive Analysis/MyFirstClass.py", line 15, in print(user1.first_name) NameError: name 'user1' is not defined ['user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'user7', 'user8', 'user9', 'user10', 'user11', 'user12', 'user13', 'user14', 'user15', 'user16', 'user17', 'user18', 'user19', 'user20', 'user21', 'user22', 'user23', 'user24', 'user25', 'user26', 'user27', 'user28', 'user29', 'user30', 'user31', 'user32', 'user33', 'user34', 'user35', 'user36', 'user37', 'user38', 'user39', 'user40', 'user41', 'user42', 'user43', 'user44', 'user45', 'user46', 'user47', 'user48', 'user49', 'user50'] My thoughts on this: I don't know if I'm correct but I have read in another thread that the object/instance of the class should not be indented as it will be read "as content of the class". On the other hand, the for-loop requires me to have this text indented. How can I solve this problem, if that what I said is exactly the problem. If what I said, is wrong, how can I then solve this NameError. Maybe it has something to do with the list from which I take the object-name. As they are strings, maybe that is the trouble-maker. I'd be thankful for any hint on how I can solve this.
Access elements of your list using index notation: print(my_new_list[0].first_name) And there is nothing wrong with your indentation. You never defined the symbol user1... You only have my_new_list which is of type list, which you can access the elements of using [id].
I used some pseudo lists to demonstrate but with this setup you would access the values like so first = ['vash', 'billy'] last = ['stampede', 'stack'] class MyClass: def __init__(self, f_name, l_name): self.first_name = f_name self.last_name = l_name self.full_name = f_name + ' ' + l_name user = [MyClass(first[idx], last[idx]) for idx, item in enumerate(first)] print(user[0].first_name) print(user[1].first_name) [print(user[idx].full_name) for idx, item in enumerate(user)] ~/python/stackoverflow/9.23$ python3.7 dicta.py vash billy vash stampede billy stack
Making QComboBox widgets distinguishable in a for loop using PyQt5
I'm having trouble with an application that creates a grid of QComboBox widgets (see picture below). I use a for loop to create the elements of the grid. QComboBox grid layout I would like to be able to treat each QComboBox separately. Here is the code without attempts to do so: grid = QGridLayout() combos = [ '1', '1', '1', '', '1', '1', '1', '1', '', '1', '1', '1', '1', '', '1', '1', '1', '1', '', '1'] positions = [(i,j) for i in range(5) for j in range(5)] for position, dcombo in zip(positions, combos): if dcombo == '': continue combo = QComboBox() for x in range(0, 30): combo.addItem(QIcon("/icons/%d.PNG" % x),"") combo.setFixedSize(120,100) combo.setIconSize(QSize(100,100)) grid.addWidget(combo, *position) comboList['combo{0}'.format(position)] = position Here is my attempt, and the point at which I am currently stuck: grid = QGridLayout() combos = [ '1', '1', '1', '', '1', '1', '1', '1', '', '1', '1', '1', '1', '', '1', '1', '1', '1', '', '1'] comboList = {} positions = [(i,j) for i in range(5) for j in range(5)] for position, drawcombo in zip(positions, combos): if drawcombo == '': continue combo = QComboBox() for x in range(0, 30): # combo.addItem(QIcon("absolver deck reviewer/icons/%d.PNG" % x),"") combo.setFixedSize(120,100) combo.setIconSize(QSize(100,100)) grid.addWidget(combo, *position) comboList['combo{0}'.format(position)] = position combo.currentIndexChanged.connect(lambda: self.logChange(comboList['combo{0}'.format(position)])) def logChange(self, currentCombo): sender = self.sender() print(str(currentCombo) + ' was changed') The print() method only ever returns the last position in the list (in this case, the (3, 4) tuple.
As the position variable changes this is overwritten by it only prints the last one, if you want it not to be overwritten you must pass it as an argument to the lambda function, besides. And for this you must also pass as an argument the variable that sends the signal, in your case use the following: combo.currentIndexChanged.connect( lambda ix, p=position: self.logChange(comboList['combo{0}'.format(p)]))
Nested dict in python, searching based on inner key get inner value and parent key
I have following dict: defaultdict(<type 'dict'>, {'11': {('extreme_fajita', 'jalapeno_poppers'): '4',('test12', 'test14'): '5'}, '10': {('jalapeno_poppers', 'test', ): '2', ('test2',): '3', ('test14',): '5'} } And I want to search on based on inner key i.e ('test2',) I should get the value from inner dictionary and parent key (outer key) i.e searching for ('test2',) I should get get ['10', '3'] or whole like ['10', '('test2', )', '3']
I'm going to assume your defaultdict looks like: defaultdict = {'11': {('extreme_fajita', 'jalapeno_poppers'): '4',('test12', 'test14'): '5'}, '10': {('jalapeno_poppers', 'test2', ): '2', ('test2',): '3', ('test14',): '5'} } If that's the case, then you can use: searchValue = 'test2'; found = [] for masterkey,mastervalue in defaultdict.iteritems(): for childkey,childvalue in mastervalue.iteritems(): if searchValue in childkey: found.append(childvalue) print found
Dictionary is not ordered so you will not get in the order as '2','3' instead you can get all values from the dictionary where 'test2' found. I have following code for this: def getKeys(d1, path="", lastDict=list()): for k in d1.keys(): if type(k) is tuple: if 'test2' in k: print "test2 found at::", path + "->" , k print "Value of test2::", d1[k] print "Values in parent::", [kl for kl in lastDict[len(lastDict)-1].values()] elif type(d1[k]) is dict: lastDict.append(d1[k]) if path == "": path = k else: path = path + "->" + k getKeys(d1[k], path) d = {'11': {('extreme_fajita', 'jalapeno_poppers'): '4',('test12', 'test14'): '5'}, '10': {('jalapeno_poppers', 'test', ): '2', ('test2',): '3', ('test14',): '5'}} getKeys(d) Output: test2 found at:: 11->10-> ('test2',) Value of test2:: 3 Values in parent:: ['2', '5', '3']