Okay, I was able to look at your file and do it in about five minutes; think about what's getting into your dictionary that shouldn't, I'll show you mine; but I want to see a bit more effort, you're close, I guess I would say, be more selective about what makes it into your dictionary...

Okay, I was able to look at your file and do it in about five minutes; think about what's getting into your dictionary that shouldn't, I'll show you mine; but I want to see a bit more effort, you're close, I guess I would say, be more selective about what makes it into your dictionary...

LOL that's my problem.. Nothing is getting into my dictionary, so i'm really confused on what it is that you mean.... Tried another approach, that does get me input for my keys, but my values are still emypty... {'214': ('', ''), '155': ('', ''), '135': ('', ''), '211': ('', ''), '573': ('', '')}

import re
d ={}
balfilename =open('balance.dat')


for line in balfilename.readlines():
    if line:
        """line=' '.join(line)"""
        line=re.sub('', '', line)
 
        line= line.rstrip()
        line_content= line.split(' ')
        print(line)
        d[line_content[0]]= (line_content[1], line_content[2])
print(d)
        

balfilename.close()

:zzz:

Do i have it????

d ={}
balfilename =open('balance.dat')


for line in balfilename.readlines():
    if line:
        line =" ".join(line.split())
        line= line.strip()
        line= line.rstrip()
        line_content= line.split(' ')
       
        d[line_content[0]]= (line_content[1], line_content[2])
print(d)
        

balfilename.close()
{'214': ('400-19-8435', '2501.14'), '155': ('972-87-1379', '3300.26'), '135': ('706-02-6945', '-99.06'), '211': ('940-32-8944', '4153.07'), '573': ('283-86-4227', '1262.31'), '554': ('549-07-8738', '-166.40'), '191': ('699-77-2796', '2565.29'), '192': ('814-77-4491', '3347.81'), '115': ('139-28-4313', '1056.30'), '271': ('302-52-0390', '-89.25'), '238': ('353-78-2979', '3960.09'), '276': ('050-44-5987', '27899.48'), '415': ('887-60-6792', '1835.59'), '395': ('645-86-7907', '10640.25'), '182': ('632-72-6766', '3516.77'), '546': ('276-69-3568', '2141.17'), '341': ('541-30-5692', '2549.50'), '522': ('236-87-3487', '37352.53'), '301': ('008-41-1683', '4120.56'), '282': ('430-00-7434', '2155.40'), '502': ('340-25-3417', '2658.43'), '361': ('484-86-7987', '4031.13'), '466': ('845-40-4968', '2906.13'), '143': ('595-74-5767', '4289.07'), '300': ('935-88-9835', '4690.44'), '208': ('265-40-8305', '1446.72'), '439': ('024-13-2629', '2754.91'), '447': ('992-07-5363', '410.49'), '380': ('881-80-3386', '3622.22'), '266': ('016-92-4542', '3436.31'), '229': ('495-33-4989', '2348.31'), '264': ('713-79-2241', '4938.17'), '219': ('794-00-0659', '3356.98'), '249': ('169-57-3539', '3927.39'), '482': ('526-11-5286', '2624.67'), '162': ('814-50-7178', '3571.94'), '220': ('909-38-6694', '4496.73'), '314': ('262-24-2379', '2977.82'), '379': ('602-90-2297', '2318.76'), '356': ('246-75-8770', '2389.04'), '532': ('918-00-7767', '10376.87'), '332': ('059-96-0121', '1164.89'), '431': ('973-40-7961', '3540.63'), '515': ('021-66-6171', '2981.30'), '376': ('921-96-7973', '3684.93')}
>>>
commented: good job putting in effort! +4

that is MUCH better, I changed the account balance in mine to a float just because I wasn't sure if you were going to change it. I broke mine down step by step because I like to "show my work" when helping out

def make_dic(file):
    with open(file,'r') as f:
        file=f.readlines()
    dic={}
    for line in file:
        line=line.split(' ')
        clean_tup=tuple()
        for num in line:
            if num!='':
                clean_tup+=(num,)
        dic[clean_tup[0]]=clean_tup[1]+' ',float(clean_tup[2].strip('\n'))
    return dic

you could also make the key an int if you wanted, and I added that touch of whitespace just because it's more appealing to me. and you could always make them print pretty if you wanted by either manipulating the format or doing a funky little number like:

>>>dic=make_dic('accounts.txt')
>>>for account in dic:
	display=''
	display+=str(account)+':'
	for data in dic[account]:
		display+=' '+str(data)
	print(display)

	
115: 139-28-4313  1056.3
143: 595-74-5767  4289.07
155: 972-87-1379  3300.26
135: 706-02-6945  -99.06

What would i change if i wanted to store the values as lists instead of tuples?

What would i change if i wanted to store the values as lists instead of tuples?

The reason i ask this is bc i just realized that i have to update the values in the dictionary according to deposits and withdraws.. Tuples are immutable aren't they??

Rebuilding the tuple from account number and saldo is not much work, but maybe it would be more natural to keep saldo as float.

d={}
line = '155 972-87-1379 3300.26\n'
line = line.strip().split()
if len(line) == 3:
     d[int(line[0])] = [line[1], float(line[2])]

What would i change if i wanted to store the values as lists instead of tuples?

well if you REALLY want to here's a hint: lis=[item for item in tup]

well if you REALLY want to here's a hint: lis=[item for item in tup]

Better not build tuple if you do not want it, but to change it to list, just tell Python to do so:

>>> t = (1,2,3)
>>> list(t)
[1, 2, 3]

well if you REALLY want to here's a hint: lis=[item for item in tup]

In order to update the key values in the dictionary, doesn't they have to be in lists though? I not, i see no reason to change it.

In order to update the key values in the dictionary, doesn't they have to be in lists though? I not, i see no reason to change it.

That's kind of an oddly worded question, did you mean update the keys or update the values? If you're talking about values, no they don't HAVE to, but it would be much easier, and more practical to change, but here's an option with tuples:

def up_bal(tup,up):
	tupe=tup[0:2]
	tupe+=tup[2]+up,
	return tupe

>>> a
('this', 'balance', 3.25)
>>> b=up_bal(a,5.23)
>>> 
>>> b
('this', 'balance', 8.48)

That's kind of an oddly worded question, did you mean update the keys or update the values? If you're talking about values, no they don't HAVE to, but it would be much easier, and more practical to change, but here's an option with tuples:

def up_bal(tup,up):
	tupe=tup[0:2]
	tupe+=tup[2]+up,
	return tupe

>>> a
('this', 'balance', 3.25)
>>> b=up_bal(a,5.23)
>>> 
>>> b
('this', 'balance', 8.48)

Sorry about that.. To make it sound more simple, i have another file with more lines of info that reads something like, but with way more lines:

380     2932.48
192     -830.84
379     2338.82
249     3444.99
466      -88.33

What i have to do is create another function that reads in the file above and then update the values in the dictionary that i just created in the other function.. The negative numbers are withdraws, and the positives are deposits...

well easy enough; I'd say lists are easier; breaking it down I would do something like "not really showing you too much new here, just showing how what I showed you before but with lists":

def make_dic(file):
    with open(file,'r') as f:
        file=f.readlines()
    dic={}
    for line in file:
        line=line.split(' ')
        clean_lis=[]
        for num in line:
            if num!='':
                clean_lis.append(num)
        dic[int(clean_lis[0])]=[clean_lis[1]+' ',float(clean_lis[2].strip('\n'))]
    return dic

def lup_bal(lis,up):
    cur=lis[-1]
    update=float(cur)+float(up)
    lis.pop()
    new=[account for account in lis]
    new.append(update)
    lis.pop()
    return new

Note that in the following I used what you posted and added one to the original file called 192, that way you could see the change, so really only pay attention to 192's change.

>>> bank=make_dic('help.txt')
>>> 
>>> 
>>> bank
{192: ['094-11-2328 ', 3322.25], 115: ['139-28-4313 ', 1056.3], 143: ['595-74-5767 ', 4289.07], 155: ['972-87-1379 ', 3300.26], 135: ['706-02-6945 ', -99.06]}
>>> 
>>> with open('help3.txt') as f:
	for line in f:
		line=line.split()
		key=int(line[0])
		value=float(line[1])
		if key in bank.keys():
			bank[key]=lup_bal(bank[key],value)

			
>>> bank
{192: ['094-11-2328 ', 2491.41], 115: ['139-28-4313 ', 1056.3], 143: ['595-74-5767 ', 4289.07], 155: ['972-87-1379 ', 3300.26], 135: ['706-02-6945 ', -99.06]}

This isn't the best or easiest way to do it, but I believe it gets the idea across.

[B]help.txt[/B]
115     139-28-4313     1056.30
135     706-02-6945      -99.06
143   595-74-5767     4289.07
155     972-87-1379     3300.26
192   094-11-2328     3322.25
[B]help3.txt[/B]
380     2932.48
192     -830.84
379     2338.82
249     3444.99
466      -88.33

well easy enough; I'd say lists are easier; breaking it down I would do something like "not really showing you too much new here, just showing how what I showed you before but with lists":

def make_dic(file):
    with open(file,'r') as f:
        file=f.readlines()
    dic={}
    for line in file:
        line=line.split(' ')
        clean_lis=[]
        for num in line:
            if num!='':
                clean_lis.append(num)
        dic[int(clean_lis[0])]=[clean_lis[1]+' ',float(clean_lis[2].strip('\n'))]
    return dic

[B]def lup_bal(lis,up):
    cur=lis[-1]
    update=float(cur)+float(up)
    lis.pop()
    new=[account for account in lis]
    new.append(update)
    lis.pop()
    return new[/B]

Note that in the following I used what you posted and added one to the original file called 192, that way you could see the change, so really only pay attention to 192's change.

>>> bank=make_dic('help.txt')
>>> 
>>> 
>>> bank
{192: ['094-11-2328 ', 3322.25], 115: ['139-28-4313 ', 1056.3], 143: ['595-74-5767 ', 4289.07], 155: ['972-87-1379 ', 3300.26], 135: ['706-02-6945 ', -99.06]}
>>> 
>>> with open('help3.txt') as f:
	for line in f:
		line=line.split()
		key=int(line[0])
		value=float(line[1])
		if key in bank.keys():
			bank[key]=lup_bal(bank[key],value)

			
>>> bank
{192: ['094-11-2328 ', 2491.41], 115: ['139-28-4313 ', 1056.3], 143: ['595-74-5767 ', 4289.07], 155: ['972-87-1379 ', 3300.26], 135: ['706-02-6945 ', -99.06]}

This isn't the best or easiest way to do it, but I believe it gets the idea across.

[B]help.txt[/B]
115     139-28-4313     1056.30
135     706-02-6945      -99.06
143   595-74-5767     4289.07
155     972-87-1379     3300.26
192   094-11-2328     3322.25
[B]help3.txt[/B]
380     2932.48
192     -830.84
379     2338.82
249     3444.99
466      -88.33

Can you comment on what exactly it is that the bold is doing?? I haven't been coding in Python for a month yet, so a most of this is still new to me.

If it easier to understand my style here is my version using what I posted in previous post:

from __future__ import print_function
import sys
from pprint import pprint

def process_line(data, bank):
    data = data.rstrip()
    line = data.split()
    account_number = int(line[0])
    if len(line) == 3:
        # establish account
        bank[account_number] = [line[1], float(line[2])]
    elif len(line) == 2:
        if account_number in bank:
             bank[account_number][1] += float(line[1])
        else:
            raise ValueError('Account does not exist in update: %i, %r' % (account_number, data))
    else:
        raise ValueError('Wrong number of items at line: %r' % data)

def apply_transactions(bank, fn):
    with open(fn) as f:
        for line in f:
            try:
                process_line(line, bank)
            except ValueError as e:
                print(e, file=sys.stderr)

bank={}
for fn in ('help.txt', 'help3.txt'):
    apply_transactions(bank, fn)
    print()
    print('Bank situation')
    pprint(bank)
    print()

Could any of you recommend a few python books that are good for my programming level(beginner)??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.