0

I am trying to produce some mechanical stress info from windspeed data for a crane boom when it is between 0 and 90 degrees, with data from each angle saved into it's own file. I have the script working fine when doing just one file/angle, however when I try and use any sort of loop to do it for all angles it will create the files, but only the first has any data in it. I am a beginner and am not very savvy with Python, so I was hoping someone could spot something simple I have missed. I have included a short example file of the source data: Windspeed source file - cut down

import math
file = open("C:/Users/Jacob/Desktop/BOM Data/HD01D_Data_074272_999999999523840.txt", 'r')

boomDirection = 0
vaneSpeed = 120
maxShear = 75.97043478
maxVonMises = 500.0216811

while boomDirection < 91:
    data_file = open("Bolt Stress - " + str(boomDirection) + " Degrees.csv", 'w')
    line = file.readline()
    line = file.readline()
    while line != '':
        try:
            if len(line.split(','))>1:                                      
                windSpeedHigh = int(line.split(',')[19])
                windSpeedLow = int(line.split(',')[22])
                windDirection = int(line.split(',')[14])
                relSpeedHigh = math.sin(math.radians((90-(boomDirection - windDirection))))*windSpeedHigh
                relSpeedLow = math.sin(math.radians((90-(boomDirection - windDirection))))*windSpeedLow
                VonMisesHigh = (maxVonMises/vaneSpeed)* relSpeedHigh
                VonMisesLow = (maxVonMises/vaneSpeed)* relSpeedLow
                data_file.write(str(round(VonMisesHigh,1)) + ('\n'))
                data_file.write(str(round(VonMisesLow,1)) + ('\n'))
        except ValueError:
            pass
        line = file.readline()
    data_file.close()
    boomDirection = boomDirection + 1
david_10001
  • 492
  • 1
  • 6
  • 22
  • 2
    after the first file `line != '':` this condition will always be false. – gvmani Jun 18 '18 at 13:21
  • Can you please show the input file data? – OneCricketeer Jun 18 '18 at 13:21
  • Yes I remember the last line in the source file being blank, which is why I included that as the point to stop. I'll try and reopen the source file inside the first loop – david_10001 Jun 18 '18 at 13:25
  • try [this](https://stackoverflow.com/questions/15599639/what-is-the-perfect-counterpart-in-python-for-while-not-eof) - to detect an End of File. You will have to change your loop though. – user2883071 Jun 18 '18 at 13:28
  • That seemed to do the trick! I didn't tell it to re-open the source data file from the start each time. Cheers – david_10001 Jun 18 '18 at 13:29

0 Answers0