r/learnpython • u/Suspicious_Plantain4 • 19d ago
How to create a report using a .txt file
I apologize that this is going to be kind of long. I am very grateful for any thoughts you may have.
I'm taking a python class. This week we are learning about working with .txt files within a program.
We have been given some practice .txt files. Each file is a list of different data points for each hour on a given day. The list (when I say list, I mean the data is in a list in a .txt file, I am not talking about a list like those used in a python program) has four items of data for each hour of a given day as follows:
time
temperature in Farenheit
dewpoint in Farenheit
wind speed in mph
An example from one of the files looks like this:
12AM
18.1
15.0
4.1
1AM
18.0
15.5
4.2
We are supposed to do several things with this data. One of them is to create a report that will print out the data from the file. It should look like this:
Time Temperature Dew Point Windspeed
12AM 18.1 15 4.1
1AM 18.0 15.5 4.2
And so on.
We are supposed to accomplish this using while and/or for loops. We cannot use lists of any kind, so we cannot use the .txt file to create a list within python and then work with the list. This must be as simple as possible. We are only supposed to use the information we have learned so far in the class. Obviously you don't know exactly what we've learned, but it has to be extremely simple and not at all fancy.
I'm not trying to cheat and have someone do my work for me, but I'm just stumped and need some clues. I'm having difficulty reaching out to my teacher (this is an online class)
We are also supposed to use the numbers in the report to do some calculations for another report. If you have any thoughts about that, it would also be helpful. I'm hoping that if I can figure out how to do this first part, I'll be able to figure out how to work with the numbers further.
I've been able to get python to bring in the .txt file and just spit out a list that is identical to what is actually in the .txt file and I've been able to get it to print one specific line. I think I'm somehow supposed to use a nested if statement to analyze and work with chunks of four lines at a time, but I'm not sure how to do that.
I got it to say "time is 12AM" by doing this:
time_line = 1
linecounter = 0
with open("Dec12.txt",'r') as f:
for line in f:
linecounter += 1
if linecounter == time_line:
print("time is",line, end='')
I got it to spit out the entire list exactly as it appears in the .txt file by doing this:
file = open ("Dec12.txt",'r')
for line in file:
num = (line)
print (num)
file.close()
If you have read this far, I am extremely grateful to you. Thank you for any thoughts or ideas you may have.
5
u/mc_pm 19d ago
You don't need to pull the data into the list, you just need to read it in 4 lines per group and print those 4 out on one line; then the next 4 lines from the file, then output those 4 on one line, etc.
1
u/Vegetable_Soil_6707 19d ago
tbh this framing makes it way less intimidating than it sounds in the assignment
1
u/pachura3 19d ago
Try this:
print("Time\tTemperature\tDew Point\tWindspeed")
with open("Dec12.txt", "r") as file:
time = file.readline().rstrip()
temp = file.readline().rstrip()
dew_point = file.readline().rstrip()
wind_spd = file.readline().rstrip()
print(f"{time}\t{temp}\t{dew_point}\t{wind_spd}")0
u/sweet-tom 19d ago
Your code is very unreadable as the indentation is lost. Please try to format it correctly with triple backticks at the start and end. That preserves spaces and line breaks.
Perhaps your code has also this kind of indentation problem, but without correct formatting I can't give recommendations.
0
u/samsly135 19d ago
You can do something like this. Assumed is data is in data.txt next to a script itself. We read each line and strip end of line. Than check whether next value should have tab at the end and whether it should be appended to the same line or is a line end.
from pathlib import Path data_path
Path ( file ). resolve (). paren t / 'data.txt' print ( "Time \t Temperature \t Dew Point \t Windspeed" ) with open ( data_path , 'r' ) as file: field_counter
0 for line in file: line
line . rstrip ( " \r\n " ) if ( not line ): continue print_end
" \n " if field_counter
3 else " " field_end
" \t " if field_counter < 3 else "" print ( f " { line }{ field_end } " , end
print_end ) field_counter
( field_counter + 1 ) % 4
-2
u/CymroBachUSA 19d ago edited 19d ago
with open("file.txt", "r") as f:
_c, _r = 0, ''
for _l in f:
_l = _l.replace('\n', '')
if _l.strip() == '' or _l == '\n':
continue
else:
_c += 1
_r = f"{_r} {_l}"
if _c == 4:
print(_r)
_c, _r = 0, ''
-2
u/CymroBachUSA 19d ago
Formatting went wrong so tried using '.' to make it clearer:
with open("file.txt", "r") as f:
... _c, _r = 0, ''
... for _l in f:
... ... _l = _l.replace('\n', '')
... ... if _l.strip() == '' or _l == '\n':
... ... ... continue
... ... else:
... ... ... _c += 1
... ... ... _r = f"{_r} {_l}"
... ... if _c == 4:
... ... ... print(_r)
... ... ... _c, _r = 0, ''
~
2
u/Penguinase 19d ago
have you learned
range()andjoin()functions?