Hi,
I am stuck with a problem and hoping someone can help.
How can I import data from a file and store them in a dictionary? I have a file named ‘bank.txt’. I need to import data and create a dictionary allowing the following features:
- Every line in the file will look like key: value pair.
- Key is a user's name and value is an amount to update the user's bank account with. The value should be a number, however, it is possible that there is no
value or that the value is an invalid number.
- If the key doesn't exist, create a new key: value pair.
- If the key does exist, increment its value with the amount.
- You should also handle cases when the value is invalid. If so, ignore that line and don't update the dictionary.
I am uploading the bank.txt file here as provided to me. Since I don’t know how to do this exercise, any sample code will be useful. Looking forward to your assistance.
Thanks and Regards,
Arvind Gupta
I am stuck with a problem and hoping someone can help.
How can I import data from a file and store them in a dictionary? I have a file named ‘bank.txt’. I need to import data and create a dictionary allowing the following features:
- Every line in the file will look like key: value pair.
- Key is a user's name and value is an amount to update the user's bank account with. The value should be a number, however, it is possible that there is no
value or that the value is an invalid number.
- If the key doesn't exist, create a new key: value pair.
- If the key does exist, increment its value with the amount.
- You should also handle cases when the value is invalid. If so, ignore that line and don't update the dictionary.
I am uploading the bank.txt file here as provided to me. Since I don’t know how to do this exercise, any sample code will be useful. Looking forward to your assistance.
Thanks and Regards,
Arvind Gupta