简体   繁体   中英

Can't split multi-line string into an array of rows

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: 

When you use for row in f_content: , you will get each line as something like "4\\n" . When you then use .splitlines() , you are getting ["4", ""] . The 4 can be converted just fine, but there is no way to convert a blank string to a float. Instead, don't do your for length in row: ; just use row directly; float() doesn't mind the newline character:

>>> x = '4\n'
>>> float(x)
4.0
>>> first, second = x.splitlines()
>>> first
'4'
>>> second
''
>>> float(first)
4.0
>>> float(second)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:

That would make your loop look like this:

for length in f_content:
    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)

I changed for row in f_content to for length in f_content so that I didn't need to replace all occurrences of length with row .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM