Question

In: Computer Science

Process each line so you create dictionary where key is name of the company and value...

Process each line so you create dictionary where key is name of the company and value is a net balance
#2.a.   First column of each row is a name of the company
#2.a.a. Utes company has different types of spelling, so you need to put all of them under the same key
#2.b.   Second column is payments in
#2.c.   Third column is payments out. Note that they can be negatives as well.
#       Hint: Absolute value could be derived by using abs( number )
#2.d.   Your net balance is sum(payments in - payments out)

in python

Solutions

Expert Solution

''' open file and paremeter is given to it is path+file name '''
f = open(r"D:\vscode\python programe\test.txt",'r')
dict={}
for x in f:
   # print(x)
   row=x.split()
   ''' 
       each company name is converted into upper case. And make company name is key. 
       And the value of the key is (payment in - payment out) which is 2nd and 3rd columns respectively in file .
   '''
        
   dict[row[0].upper()]=int(row[1])-int(row[2]) 
for key,value in dict.items():
    print(key,": ",value)       # printing dictonary

First, open the file using the "open()"function and give the path of the file as a parameter.and store file content in "f" variable.
after that read " f " line by line as a file.
Create a dictionary and store data f[0] contain File name , F[1] is payment_in and f[2] is payment_out.
make f[0] as key and f[1]-f[2] is value of the key.

Note:

For running in your computer you have to change the path of the file where your file is located. and my file name is test.txt.

f = open(r"your file path\file name",'r')

my demo file:

test.txt

company_A 1000 900
company_B 800 1100
company_C 1200 1400
company_D 1500 1000
company_E 1250 1000
company_F 1400 1500
company_G 1500 1400
company_H 1100 1300


Related Solutions

PYTHON. Create a function that accepts a filename where in each line there is a name...
PYTHON. Create a function that accepts a filename where in each line there is a name of a type of bird. use a python dictionary in order to count the # of time each bird appears. (1 line = 1 appearance) Loop over your dict. to print each species and the # of times it was seen in the file once all lines have been counted. return dictionary containing count the filename opens this: blackbird canary hummingbird canary hummingbird canary...
#This is what you have to do: #1.Open the testFile.csv #2.Process each line so you create...
#This is what you have to do: #1.Open the testFile.csv #2.Process each line so you create dictionary where key is name of the company and value is a net balance #2.a. First column of each row is a name of the company #2.a.a. Utes company has different types of spelling, so you need to put all of them under the same key #2.b. Second column is payments in #2.c. Third column is payments out. Note that they can be negatives...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist; 2. add_new_item(dictionary,key,value): a function that adds...
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Create a binary search tree of stacks where each node contains a key and a stack.
IN C++   Create a binary search tree of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key, and a empty...
Use a dictionary to represent a directed graph in which each key is a pair (tuple)...
Use a dictionary to represent a directed graph in which each key is a pair (tuple) of two nodes, with the corresponding value set to the edge weight. For example, W[u,v] =42. Where in u → v. Write a function to calculate the in and out degree of a given node. What would be the advantages and disadvantages of this representation? in python
python pls create a function party_place: that search the dictionary and figure out where they party...
python pls create a function party_place: that search the dictionary and figure out where they party in that day. For example def party_place(dict2: dict, date: (int,int,int)): dict1= {'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output print(party_place(dict1, (2000,5,20,720)) = ['fire1', 'fire2'] print(party_place(dict1, (2000,5,21,720)) = ['fire1'] print(party_place(dict1, (2000,5,22,720)) = []
Write a python program that keeps names and email addresses in a dictionary as key-value pairs....
Write a python program that keeps names and email addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a person’s email address, add a new name and email address, change an existing email address, and delete an existing name and email address. The program should bind the dictionary and save it to a file when the user exits the program. Each time the program starts, it should retrieve the dictionary...
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names...
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names to addresses. A person's name (stored as a string) will be the KEY and that person's address (stored as a string) will be the VALUE. 1.2) Insert into the dictionary 'addresses' the names and addresses of two (possibly imaginary) friends of yours. 1.3) Create a second empty dictionary called 'ages'. The dictionary you just created will map names to ages. A person's name (stored...
1. Use the get() method to print the value of the "name" key and the value...
1. Use the get() method to print the value of the "name" key and the value of the "age" key of the stuInfo dictionary. Use the variable given below: stuInfo = {'name': 'John Smith', "gpa": 3.456, "age": 20} 2. Use the dict() function to make a copy of the NY dictionary to NewYorkState dictionary. 3. Change the "name" value from "John Smith" to "James Bond" of the stuInfo dictionary. Use the variable given below: stuInfo = {'name': 'John Smith', "gpa":...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT