Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Writing to a text file then finding highest value

I’m trying to run my program 5 times and each time I will end up with a different answer for amount of rows my excel file has. Example: Run 1 will return 30 rows, Run 2 will return 15, Run 3 will return 50, etc. This I got down on how to find out how many rows there are.

I’m thinking I can store these results into a .txt file so I can pull them later but primarily want it for the largest result (in this case 50).

So currently this is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import openpyxl as op

wb = op.load_workbook(r'file.xlsx', data_only=True)
sheet = wb["Sheet1"]

smax = sheet.max_row

numbers = []
total = numbers.append(str(smax))

file = open(r'files.txt', 'a+')
file.writelines(numbers)
file.close

I’m getting an error of 'NoneType' object is not iterable even though smax is an int.

Edit: fixed the error, lists are str type while I had smax as ints.

>Solution :

Your question isn’t very precise and helpful. Regardless I was able to make do a little bit with your code.

Consider documenting more clearly your code, and researching more about your question as I’m sure you would have found help. Read Documentation, this thing which everyone is supposed to read.

This site is not about fixing people’s projects but about finding solutions, to intermediate steps in this case.

  1. First, unless you are going to use iteration over a portion of this code, there is no need to have a numbers array/list. Thus, file.writelines(), becomes file.write(), as we are not writing a value from list, but that value directly.

  2. Also, no need to int() your variable, since it is already an int. Actually you were to supposed to turn it into a string to use file.write()

  3. You forgot to call close on your file which leaves the file opened. Not closing a file after usage is very dis-recommended, as Python does not guarantee to close it after your script has finished. In theory, running this script many more times could make your system unable to open anymore file. file.close -> file.close()

  4. You should add "\n" after your number which is a newline character. Without it, file.txt will contain on its first run: 30, second run: 3015, third: 301550. You see the picture… the numbers concatenate together, since they have no delimiter in between.

A working snippet, using the four points above:

import openpyxl as op

wb = op.load_workbook('file.xlsx', data_only=True)
sheet = wb["Sheet1"]

smax = sheet.max_row

line = str(smax)+"\n"

file = open(r'files.txt', 'a+')
file.write(line)
file.close()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading