17.4k views
5 votes
Please help me understand what I'm doing wrong in my python code, its returning a [] in the written file.

# You work at a low latency trading firm and are asked to deliver the order book data provided in order_book_data.txt to a superior
# The problem is that the data isn't formatted correctly. Please complete the following steps to apropriately format the data
# Notice, the first column is a ticker, the second column is a date, the third column is a Bid, the fourth column is an Ask, and the fifth column is a currency type
# 1. Open order_book_data.txt
# 2. Remove the order book lines. i.e. ***** Order Book: ###### *****
# 3. Get rid empty lines
# 4. Get rid of spaces
# 5. Notice that there are two currencies in the order book; USD and YEN. Please convert both the Bid and Ask price to USD (if not already)
# The Bid and Ask are the 3rd and 4th column, respectively
# 6. Create a header line Ticker, Date, Bid, Ask
# 7. Save the header line and propely formatted lines to a comma seperated value file called mktDataFormat.csv

Please help me understand what I'm doing wrong in my python code, its returning a-example-1
Please help me understand what I'm doing wrong in my python code, its returning a-example-1
Please help me understand what I'm doing wrong in my python code, its returning a-example-2

1 Answer

1 vote

Answer:

There are several issues with the code that could be causing it to return an empty file.

  1. Indentation errors: Python relies on indentation to indicate blocks of code, so it's important to make sure that the code is indented correctly. In your code, the if statement and columnA = float(columnA) line are not indented properly.
  2. Missing append statement: After converting the Bid and Ask prices to USD, the code is not actually adding them to the data list.
  3. Writing the wrong variable: In the last line of the code, you are writing the entire data list as a string, instead of iterating through the list and writing each element as a string.

Step-by-step explanation:

file = open("order_book_data.txt", "r")

data = []

lines = file.readlines()

for line in lines:

if line.strip() == '':

continue

if ("***" in line):

continue

column = line.strip().split(",")

columnA = float(column[2])

columnB = float(column[3])

if column[4] == "YEN":

columnA = columnA * 0.0075

columnB = columnB * 0.0075

column[2] = str(columnA)

column[3] = str(columnB)

data.append(",".join(column))

header = "Ticker,Date,Bid,Ask\\"

file.close()

file2 = open("mktlinesFormat.csv", "w")

file2.write(header)

for line in data:

file2.write(line + "\\")

file2.close()

In this code, i use continue statements to skip over the order book lines and empty lines. then i split each line into a list of columns, convert the Bid and Ask prices to USD if necessary, and join the columns back into a comma-separated string. then append the formatted string to the data list.

After processing all the lines, i write the header line and each formatted line to a new file, one line at a time. Note that add a newline character \\ to the end of each line to ensure that each line is written on a separate line in the file.

User William Walseth
by
8.1k points