Answer:
There are several issues with the code that could be causing it to return an empty file.
- 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.
- Missing append statement: After converting the Bid and Ask prices to USD, the code is not actually adding them to the data list.
- 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.