How to save the list in to .txt? - python

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))

Related

Taking values out from a dictionary and storing it in a variable

What am I doing wrong here? I am getting errors when I run it. It says I can't convert string type to float. I want to store the values from C1 to c1 variables as float type for calculations.
import csv
file = open('Items.csv')
reader = csv.reader(file, delimiter=',')
items = dict()
headersRead = False
headers = []
for row in reader:
if headersRead == False:
for i in range(len(row)):
items[row[i]] = []
print(row[i])
headers = row
print(headers)
headersRead = True
else:
for i in range(len(row)):
items[headers[i]].append(row[i])
for key in items:
c1 = float(items[key][0])
c2 = float(items[key][1])
c3 = float(items[key][2])
constant = float(items[key][3])
file.close()
This is the csv file I am working with.
Item,C1,C2,C3,Constant
Guitar Hero,-0.1111,0,-0.2,10
iPhone 7,-0.1,-0.2,-0.33333,3
iPhone SE,-0.889,-0.23,-0.5,2
Star Wars,-0.0778,-0.373333333,-0.5,4
Markers,-0.667,-0.488333333,-0.65,3
Avengers,-0.556,-0.603333333,-0.756667,5
Elf on the Shelf,-0.04,-0.718333333,-0.863334,1
Pool Cue,-0.334,0,0,9
Tire Repair Kit,-0.223,-0.948333333,-0.076668,6
Silly Putty,-0.112,-0.063333333,-0.183335,1
Nike,-0.123,-0.178333333,0,5
This is the dictionary(items) u have used to iterate the last for loop
{'Item': ['Guitar Hero', 'iPhone 7', 'iPhone SE', 'Star Wars', 'Markers', 'Avengers', 'Elf on the Shelf', 'Pool Cue', 'Tire Repair Kit', 'Silly Putty', 'Nike'], 'C1': ['-0.1111', '-0.1', '-0.889', '-0.0778', '-0.667', '-0.556', '-0.04', '-0.334', '-0.223', '-0.112', '-0.123'], 'C2': ['0', '-0.2', '-0.23', '-0.373333333', '-0.488333333', '-0.603333333', '-0.718333333', '0', '-0.948333333', '-0.063333333', '-0.178333333'], 'C3': ['-0.2', '-0.33333', '-0.5', '-0.5', '-0.65', '-0.756667', '-0.863334', '0', '-0.076668', '-0.183335', '0'], 'Constant': ['10', '3', '2', '4', '3', '5', '1', '9', '6', '1', '']}
In the first key("Item") in the dictionary, which has a list of strings as its value. So I have added a if-continue statement, which skips to the next iteration of the loop if the key is "Item".
import csv
file = open('Items.csv')
reader = csv.reader(file, delimiter=',')
items = dict()
headersRead = False
headers = []
for row in reader:
if headersRead == False:
for i in range(len(row)):
items[row[i]] = []
print(row[i])
headers = row
print(headers)
headersRead = True
else:
for i in range(len(row)):
items[headers[i]].append(row[i])
for key in items:
if key == 'Item':
continue
c1 = float(items[key][0])
c2 = float(items[key][1])
c3 = float(items[key][2])
constant = float(items[key][3])
file.close()
Unfortunately, I couldn't add images for explanation as this is my first answer on Stack overflow.

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

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)]))

How do you append multiple keys to a value in a list?

It works, but how can I add multiple values to a key?
This is my code:
for x in range(number_of_wall):
wall = 1
wall_area = input("What is the area of wall {}" .format(wall) + " in " + room_name + ": ")
dimention.append([room_name, wall_area])
wall = wall + 1
print(dimention)
And this is what it comes up with:
[['Lounge', '13'], ['Lounge', '13'], ['Lounge', '13'], ['Lounge', '13'], ['Bedroom', '14'], ['Bedroom', '14'], ['Bedroom', '14'], ['Bedroom', '14']]
How can I modify my code so it comes up with:
[['Lounge': '13', '13', '13', '13'], ['Bedroom': '14', '14', '14', '14']]
Or something along those lines. Thanks.
You have to use dictionary in place of list for dimention:
dimention = {}
for x in range(number_of_wall):
wall = 1
wall_area = input("What is the area of wall {}" .format(wall) + " in " + room_name + ": ")
if room_name not in dimention:
dimention[room_name] = [wall_area]
else:
dimention[room_name].append(wall_area)
wall = wall + 1
print(dimention)
In this case, check for room_name present in the dimention if not add room_name as key and assign first value of wall_area; otherwise append wall_area to the existing room_name list.
You can also use set instead of dictionary.

Categories