I'm very new to Python and just ran into a problem.
I tried many solutions suggested (mostly from this question) on making every line of a .txt document into an array object. I tried using just split()
, split("\n")
and splitlines()
, but none of them work. Every line in the text document is a number and it will do some calculations on it. For example the first line is 50, but it does the first calculation on the number 5, second one on the number 0 and on the next one it throws an error about being unable to convert it into a float (ValueError: could not convert string to float), probably cause it tried to convert \n or something.
Here is the code:
def weightTest(f, minWeight, fti):
weights = []
f_content = open(f, encoding='UTF-8')
for row in f_content:
length = row.splitlines()
for length in row:
weight = float(length) ** 3 * fti / 100 # ValueError
if weight < minWeight:
print("Smaller than the minimum weight.")
else:
print("The weight is " + str(weight) + " grams.")
weights.append(weight)
print("The biggest weight: " + str(max(weights)) + " kg")
f_content.close()
f = input("Insert file name: ")
alam = float(input("Insert minimum weight: "))
fti = float(input("Insert FTI: "))
weightTest(f, alam, fti)
Here is the text used (instead of spaces, there's new lines, StackOverflow doesn't want to show them somewhy): 50 70 75 55 54 80
Here is the log:
Insert file name: kalakaalud.txt
Insert minimum weight: 50
Insert FTI: 0.19
Smaller than the minimum weight.
Smaller than the minimum weight.
Traceback (most recent call last):
File "C:\File\Location\kalakaal.py", line 18, in <module>
weightTest(f, minWeight, fti)
File "C:\File\Location\kalakaal.py", line 7, in weightTest
weight = float(length) ** 3 * fti / 100
ValueError: could not convert string to float: