(Apr-29-2022, 08:10 PM)deanhystad Wrote: Quote:The program generates a results file with different trade statistics and parameters. That is where I got a lot of unnecessary decimal output that I figured I could clean up by converting to int so there would be no decimals.
But you are not doing that here. Calling int(float(datalist[2])) does not replace the contents of datalist[2] with an integer, it creates an integer object using the float value of datalist[2]. The integer object is used in the comparison and deleted. datalist[2] is unchanged.
I'm getting the chance today to go back and take a closer look at this.
I imported a line from the data file with this:
import glob, os
num_lines = 0
for file in glob.glob("*.csv"):
barfile = open(file, "r")
for line in barfile:
if num_lines < 1:
stats = line.split(",")
num_lines +=1
else: break #not just break (need the else)
break
print(f'Successfully exited iteration loop with num_lines = {num_lines}')
Now I have a list of elements as stats:
['2527373', '17169.0', '1081.0', '2257.83', 'SPX 191220P02125000', '18250.0', '2125.0', '239.8', '0.201915', '-0.355646', '13.874128', '0.000455', '-0.129162\n']
They're all <class 'str'> . Some are numbers. Some numbers I can leave as integers (e.g. stats[2], stats[5], stats[6]). For others, I need the decimal part (e.g. stats[7:]).
If I try this:
for i in range(5,len(stats)):
conv_element = int(stats[i])
print(f'Element #{i} is {conv_element} and data type {type(conv_element)}.')
I get: ValueError: invalid literal for int() with base 10: '18250.0'
If I use:
conv_element = int(float(stats[i]))
Then it successfully converts to integer. I think that's why I felt the need to use int(float()). The numbers are used for computation and then sometimes printed to a results file. It's then onto the next row of the data file. I'm not looking to make any changes to the original data file.