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?
Write a code in python to read in a file of spice like component and/or control...
Write a code in python to read in a file of spice like component and/or control data. break the data up into fields in a class object.
Using Python read dataset in the HTML in beautiful way. You need to read CSV file...
Using Python read dataset in the HTML in beautiful way. You need to read CSV file ( Use any for example, You can use small dataset) You need to use pandas library You need to use Flask Make search table like YouTube has.
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?
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
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?
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) Write...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT