Question

In: Computer Science

2. Combine multiple files Use Python programming to combine two text files: customer-status.txt and sales.txt Data...

2. Combine multiple files

Use Python programming to combine two text files: customer-status.txt and sales.txt

Data columns in customer-status.txt (separated by comma):

Account Number, Name, Status 527099,Sanford and Sons,bronze

Data columns in sales.txt (separated by comma):

Account Number, Name, SKU, Quantity, Unit Price, Ext Price, Date

163416,Purdy-Kunde,S1-30248,19,65.03,1235.57,2014-03-01 16:07:40 527099,Sanford and Sons,S2-82423,3,76.21,228.63,2014-03-01 17:18:01

After you combine, you will see the following:

527099,Sanford and Sons,S2-82423,3,76.21,228.63,2014-03-01 17:18:01,bronze

Solutions

Expert Solution

Here is the code :

note : put the customer-status.txt and sales.txt in the same folder as the python file

import re 
customer_status = open("customer-status.txt", "r")
cust_arr=[]
sales_arr=[]
new_file_arr=[]
for line in customer_status:
    temp=[]
    line=line.split(',');
    for sub in line:
        temp.append(re.sub('\n','',sub))
    cust_arr.append(temp);
customer_status.close()
sales=open("sales.txt","r")
for line in sales:
    temp=[]
    line=line.split(',');
    for sub in line:
        temp.append(re.sub('\n','',sub))
    sales_arr.append(temp);
sales.close();
for i in range(1,len(cust_arr)):
    for j in range(1,len(sales_arr)):
        if cust_arr[i][0]==sales_arr[j][0]:
            sales_arr[j].append(cust_arr[i][2])
            new_file_arr.append(sales_arr[j])
new_file_str="";
for line in new_file_arr:
    for word in line:
        new_file_str=new_file_str+word+','
    new_file_str=new_file_str+'\n'
print(new_file_str)


new_file = open('output.txt', 'w')
new_file.write(new_file_str)
new_file.close()

Related Solutions

This is JAVA PROGRAMMING Sort the contents of the two files in ascending order and combine...
This is JAVA PROGRAMMING Sort the contents of the two files in ascending order and combine them into one new file (words.txt). When comparing string order, you must use the compareTo method and make an exception.The source code below is a code that only combines files. Use the comparedTo method to sort the contents of the two files in ascending order.(ex. if(str1.compareTo(str2)<0) {}) <file1.txt> at first   castle   consider   considerable   enlighten   explain   explanation   female   <file2.txt> consideration   considering that   education   educational   endow  ...
In Python This assignment involves the use of text files, lists, and exception handling and is...
In Python This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>>   Enter gender...
a python function that reads two text files and merges in to one Linked List, be...
a python function that reads two text files and merges in to one Linked List, be able to print each Item in the new single Linked List class Node(object): item = -1 next = None def __init__(self, item, next): self.item = item self.next = next ================================ textfile! 979 2744 5409 1364 4948 4994 5089 703 1994 4637 2228 4004 1088 2812 170 5179 2614 238 4523 4849 3592 3258 1951 3440 3977 1247 4076 1824 4759 4855 5430 347 974...
Python programming ***************************************************************************************************** If we load the json files then how could we use the objects...
Python programming ***************************************************************************************************** If we load the json files then how could we use the objects to extract particular values from those files. eg: fi1 = open("king.json", "rb") obj1 = yaml.safe_load(fi1.read()) fi1.close() fi2 = open("queen.json", "rb") obj2 = yaml.safe_load(fi2.read()) fi2.close() Now if the JSON file queen.json had elements like name, rule year, children, etc. and we wanted the name of the queen who ruled between particular years, how would we do it? second would be getting a value common for...
I have multiple .txt files in a folder, and i want to combine all their contents...
I have multiple .txt files in a folder, and i want to combine all their contents into a single .txt file How do I go about such an operation in Python?
Write a python program function to check the frequency of the words in text files. Make...
Write a python program function to check the frequency of the words in text files. Make sure to remove any punctuation and convert all words to lower case. If my text file is like this: Hello, This is Python Program? thAt chEcks% THE freQuency of the words! When is printed it should look like this: hello 1 this 1 is 1 python 1 program 1 that 1 checks 1 the 2 frequency 1 of 1 words 1
This is for Python programming, and I am trying to use 2 for loops to ask...
This is for Python programming, and I am trying to use 2 for loops to ask a slaesperson how many of 5 different items they sold, then the program is to calculate the total dollar amount sold. Formatting and all that aside, I am having an issue with the loops not stepping through the 2 lists at the same time. The first loop is taking all 5 entered quantities times the price of the first item, and then all 5...
Use python programming to write this code and provide a screen short for the code. 2....
Use python programming to write this code and provide a screen short for the code. 2. Write a function that takes one argument (a string) and returns a string consisting of the single character from that string with the largest value. Your function should contain a for loop. You can assume that the input to your function will always be a valid string consisting of at least one character. You can assume that the string will consist only of lower-case...
In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
This assignment involves the use of text files, lists, and exception handling and is a continuation...
This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>> Enter gender (boy/girl): boy...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT