Question

In: Computer Science

PYTHON #What you need to do is to transform the file Mkt_data_test.txt #to the format like...

PYTHON
#What you need to do is to transform the file Mkt_data_test.txt
#to the format like in file in the screenshot
#
#Your steps to get there:
#1.Create a header line with following columns: Time;Bid\Ask;Price;Volume. NOTE NO SPACES
#2.Remove all of the timestamp lines, i.e ======== Data: .....
#3.Remove the 1900-01-01 from the timestamp but leave the time itself
#4.Get rid of all spaces and empty lines
#5.Replace 0 or 1 in the second position with Bid or Ask, Bid for 0, Ask for 1
#6.Save the header line and propely formatted lines to the COMMA-SEPARATED csv file named mktDataFormat.csv

The txt file is:

======== Data: 00:05:08.627012 =========          
         
1900-01-01 00:05:08.627012 ; 0 ; 1.16198 ; 10000000.0
1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.0
         
========= Data: 00:05:12.721536 =========        
         
1900-01-01 00:05:08.627012 ; 0 ; 1.16198 ; 10000000.0
1900-01-01 00:05:12.721536 ; 0 ; 1.16209 ; 1000000.0
1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.0
         
========= Data: 00:05:12.729989 =========        
         
1900-01-01 00:05:08.627012 ; 0 ; 1.16198 ; 10000000.0
1900-01-01 00:05:12.721536 ; 0 ; 1.16209 ; 1000000.0
1900-01-01 00:05:12.729989 ; 1 ; 1.16218 ; 1000000.0
1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.0
         
========= Data: 00:05:12.735727 =========        
         
1900-01-01 00:05:08.627012 ; 0 ; 1.16198 ;  10000000.0
1900-01-01 00:05:12.735727 ; 0 ; 1.16208 ; 1000000.0
1900-01-01 00:05:12.729989 ; 1 ; 1.16218 ; 1000000.0
1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.

What I have so far is this:

csvRead = open("Mkt_data_test.txt","r")
lines = csvRead.readlines()

for line in lines[1: ]:
    string =
    string = string.replace("1900-01-01", "")
    string = string.replace("00:","")
    string = string.replace(' ', '')
    line = string.split(';')
    columns = line
    colA = columns[0]
    colB = columns[1]
    colC = columns[2]
    allColumns = colA + "." + colB + "." + colC
    if line[1] == "1":
        line[1] = line[1].replace("1", "Ask")
        print(line)
        if line[1] == "0":
            line[2] = line[2].replace("0", "Bid")
            print(line)
    print(colA,colB,colC)
    #maybe get rid of line below this
    def NewArray(data):
        return line
    print (NewArray(line))
NewArray(line).append(allColumns)

csvRead.close()


csv = open("CSV.csv","w")
header = "Time;Bid\Ask;Price;Volume\n"
csv.write(header)
csv.read(NewArray(line))
csv.close()

I know I need to put the string = to something, but I can't tell what. I know there are some other issues too though and I can't figure out what!

Solutions

Expert Solution

Python code :

import csv

# read the input file 'Mkt_data_test.txt'
with open('Mkt_data_test.txt', 'r') as f, open("edited_file.txt","w") as outfile:
    for line in f:
        #Remove all of the timestamp lines
        if not line.startswith('====='):
            #Remove empty lines
            if not line.isspace():
                #Remove the 1900-01-01
                 l1 = line.replace('1900-01-01 00:', '')
                 #Remove all spaces 
                 l2 =  l1.replace(' ', '')
                 #Replace 0 or 1 in the second position with Bid or Ask, Bid for 0, Ask for 1
                 col = l2.split(";")
                 if(col[1] == '0'):
                     col[1] ='Bid'
                 if(col[1]=='1'):
                     col[1] = 'Ask'
                 l3= ';'.join(col)    
                 #write result to another file 'edited_file.txt'
                 outfile.write(l3)

#convert text file to csv file
with open('edited_file.txt', 'r') as input_file:
    stripped = (line.strip() for line in input_file)
    lines = (line.split(";") for line in stripped if line)
    with open('output.csv', 'w') as output_file:
        writer = csv.writer(output_file)
        #Create a header line and write it to a csv file
        writer.writerow(('Time', 'Bid\Ask', 'Price', 'Volume'))
        writer.writerows(lines)

       
           

SCREENSHOT 1: INPUT FILE

SCREENSHOT 2 : INTERMEDIATE EDITED FILE

SCREENSHOT 3 : OUTPUT CSV FILE

SCREENSHOT 4: OUTPUT CSV FILE


Related Solutions

Do we need Tort Reform? If not, why not? If so, what reform do you like...
Do we need Tort Reform? If not, why not? If so, what reform do you like the best?
In Python And Use List comprehension to solve this question Given a file like this: Blake...
In Python And Use List comprehension to solve this question Given a file like this: Blake 4 Bolt 1 De Grasse 3 Gatlin 2 Simbine 5 Youssef Meite 6 Your program when completed should produce output like this: >>> In the 2016 100 yard dash the top finishers were: Blake Bolt De Grasse Gatlin Simbine Youssef Meite The people with a two part name are: ['De Grasse', 'Youssef Meite'] The top three finishers were: ['Bolt', 'De Grasse', 'Gatlin'] Your mission...
Show that Christoffel symbols do not transform like a tensor. And, how does the determinant of...
Show that Christoffel symbols do not transform like a tensor. And, how does the determinant of the metric tensor transform general under coordinate transformation?
What do you know about programming in Python? What is the difference between Python and Java?
What do you know about programming in Python? What is the difference between Python and Java? What does the term Open Source mean? Name four examples of Open Source software. What is the IDEL programming environment? How does IDEL relate to Python? How do you spread a long statement over multiple lines in Python? How do you use the loop-index? How will knowing and understanding Python impact what you do in your profession and/or personal experiences?
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
Describe a dysfunctional school system. What would you do to transform the dysfunctional system into a...
Describe a dysfunctional school system. What would you do to transform the dysfunctional system into a functional system?
Describe a dysfunctional system. What would you do to transform the dysfunctional system into a functional...
Describe a dysfunctional system. What would you do to transform the dysfunctional system into a functional system?
Describe a dysfunctional system. What would you do to transform the dysfunctional system into a functional...
Describe a dysfunctional system. What would you do to transform the dysfunctional system into a functional system?? Families and Poverty
This is for python coding First of all, you need to know what Natural Numbers are....
This is for python coding First of all, you need to know what Natural Numbers are. They are the positive integers (whole numbers) 1, 2, 3, etc., and zero as well. For this project, there are TWO PORTIONS: PORTION 1: Write a program to find the SUM of the first n natural numbers the user wants to add. The program should first prompt the user for how many natural numbers are to be summed. The use for loop to do...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT