Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
temp analysis
#1
Hi everyone, me and my brother both started learning python about 2 months ago. we have bit understanding of C but just basics to be honest, so both of us every month set a challenge for ourselves, we done ok for the last month challenge but this month I guess we wen a bit ambitious. we decided to extract the data from a csv file and then convert it into a text file.now we want to analyse the data in this file. so the challenge that we set was to see that how many times the temperature goes below 17 degree, how many times the temperature goes above 24 degrees and each time when temperature goes out of this range(either blow 17 or above 24)how long it takes for the temperature to come back in this range. we also wanted to test that what is the average temperature. well as you can see it is clearly very ambitious specially when we just started learning python 2 months ago. but now we both came to a dead end. we don't know how to solve this its just getting like a maze. So, we decided to give up and seek help and I couldn't see any better place to ask this than this place.

please give us a helping hand with this puzzle. we are really hoping that we can get to the bottom of this riddle.
I have attached the code that we worked on from last 16 days and the txt file as-well that we extracted from the csv file but we really can't go through it anymore.
we're completly stuck.

I thank you all in anticipation.


import csv

with open ('Data.csv') as file:
    reader = 0
    reader = csv.DictReader(file)
    with open ('timeTemp2.txt', 'w') as f:
        
        
        file_ch_required = str (file.readline())

        while len(file_ch_required) > 0:
            for line in file:
                f.write(line)
            
            file_ch_required = str (file.readline())
            
    


timeTemp2 = open ('timeTemp2.txt', 'r')
for line in timeTemp2:
    #line.split(',')
    tT =  timeTemp2.readline()
    split_tT = tT.split(',')
    
    if split_tT[2] < 17 :
        cold=0
        cold = cold + 1
        
    elif split_tT[2] > 24 :
        hot = 0
        hot = hot+ 1
        
    elif split_tT[2] > 17 and tT < 24 :
         in_range = 0
         in_range = in_range +1
    else:
        print ('cannot find the way to do it')

print (cold, 'times less than 17')
print (hot, 'times more than 24')
print (in_range, 'times in range')
    
timeTemp2.close()
TXT FILE.


00:00:00,14 C,87�%,WSW,6�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:00:30,11 C,81�%,WSW,6�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:01:00,23 C,93�%,W,7�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:01:30,25 C,93�%,WSW,7�mph,0�mph,29.0�in,0.0�in,0.0�in,Mostly Cloudy
00:02:00,18 C,93�%,WSW,9�mph,0�mph,29.0�in,0.0�in,0.0�in,Light Rain Shower
00:02:30,10 C,93�%,W,9�mph,0�mph,29.0�in,0.0�in,0.0�in,Light Rain Shower
00:03:00,17 C,93�%,W,9�mph,0�mph,29.0�in,0.0�in,0.0�in,Mostly Cloudy
00:03:30,17 C,93�%,W,9�mph,0�mph,29.0�in,0.0�in,0.0�in,Light Rain Shower
00:04:00,23 C,100�%,W,8�mph,0�mph,29.0�in,0.0�in,0.0�in,Mostly Cloudy
00:04:30,26 C,100�%,W,10�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:05:00,20 C,100�%,WNW,13�mph,0�mph,29.1�in,0.0�in,0.0�in,Light Rain
00:05:30,21 C,93�%,W,10�mph,0�mph,29.1�in,0.0�in,0.0�in,Light Rain
00:06:00,25 C,93�%,W,9�mph,0�mph,29.1�in,0.0�in,0.0�in,Light Rain
00:06:30,25 C,100�%,W,13�mph,0�mph,29.1�in,0.0�in,0.0�in,Light Rain
00:07:00,27 C,93�%,W,14�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:07:30,27 C,93�%,W,13�mph,0�mph,29.1�in,0.0�in,0.0�in,Fair
00:08:00,14 C,93�%,W,15�mph,0�mph,29.1�in,0.0�in,0.0�in,Partly Cloudy
00:08:30,13 C,93�%,W,12�mph,0�mph,29.1�in,0.0�in,0.0�in,Fair
00:09:00,14 C,93�%,W,14�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair
00:09:30,28 C,93�%,W,16�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair
00:10:00,10 C,87�%,W,17�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair
00:10:30,18 C,81�%,W,20�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair
00:11:00,27 C,81�%,W,21�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair / Windy
00:11:30,15 C,76�%,W,22�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair / Windy
00:12:00,28 C,76�%,W,23�mph,0�mph,29.3�in,0.0�in,0.0�in,Partly Cloudy / Windy
00:12:30,17 C,70�%,W,18�mph,0�mph,29.3�in,0.0�in,0.0�in,Fair
00:13:00,23 C,70�%,W,21�mph,0�mph,29.3�in,0.0�in,0.0�in,Partly Cloudy / Windy
00:13:30,23 C,66�%,W,21�mph,0�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy / Windy
00:14:00,18 C,61�%,W,23�mph,0�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy / Windy
00:14:30,14 C,66�%,W,22�mph,33�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy / Windy
00:15:00,28 C,66�%,WSW,18�mph,0�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy
00:15:30,18 C,66�%,WSW,16�mph,30�mph,29.3�in,0.0�in,0.0�in,Showers in the Vicinity
00:16:00,29 C,61�%,W,18�mph,32�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy
00:16:30,21 C,70�%,WSW,17�mph,29�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:17:00,12 C,70�%,WSW,16�mph,30�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:17:30,22 C,76�%,WSW,17�mph,0�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:18:00,22 C,87�%,WSW,18�mph,38�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:18:30,12 C,93�%,WSW,20�mph,35�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:19:00,24 C,87�%,W,25�mph,0�mph,29.3�in,0.0�in,0.0�in,Light Rain / Windy
00:19:30,28 C,93�%,W,31�mph,43�mph,29.3�in,0.0�in,0.0�in,Light Rain / Windy
Reply
#2
to start with, a csv file is a text file formatted by delimiters from start.
you can read and display contents simply by:
with open ('Data.csv') as file:
    for line in file:
        print(line)
using csv reader makes it easier as you can read as a list (row), or even as a dictionary (if it contains a header).
Reply
#3
o right I thought that it will be easier if I just convert the csv file into txt so it may give some advantage in solving.
but then how can I limit the data that I want to read in the file? like let say I want to see in each line if the temperature is less than 17 or greater than 24 and if it is I want to see that how long it takes for the temperature to come back between 17 and 24,the time interval between each row is approx 30 sec. this all info is in maybe first 10 to 15 characters of each line. but I just don't know how can we do it ?
Reply
#4
Can you post original file before messing with it,so can look at fixing encoding.
In general try use utf-8 in and out('w') when working with files.
with open ('Data.csv', encoding='utf-8' ) as file: 
We count from 0 in Python as in most programming languages.
So it should be split_tT[1],and have to clean up so get integer before compare.
# Your code
split_tT = tT.split(',')     
if split_tT[2] < 17 :
Example:
>>> s = '00:00:00,14 C,87%,WSW,6mph,0mph,29.1in,0.0in,0.0in,Mostly Cloudy'
>>> s = s.split(',')
>>> s
['00:00:00',
 '14 C',
 '87%',
 'WSW',
 '6mph',
 '0mph',
 '29.1in',
 '0.0in',
 '0.0in',
 'Mostly Cloudy']
>>> s[0]
'00:00:00'
>>> s[1]
'14 C'

>>> # Clean up so get 14 as integer
>>> new = int(s[1].split()[0])
>>> new
14
>>> type(new)
<class 'int'>

>>> # Now can compare
>>> new < 17
True
Reply
#5
thanks for that I'll use encoding from next time, I get the
new = int(s[1].split()[0])
like I think its sub splitting? but just having a problem understanding that when you have the string s, this is the single string , can you please tell me how can I repeat this for whole file and do comparison ? and apologies for the messy code as we both are just new to this , please feel free to cut out unnecessary junk out of it.

thanks in anticipation

this is the very first file 'Data.csv'. it has a header.



Time,Temperature,Humidity,Wind,Wind Speed,Wind Gust,Pressure,Precip.,Precip Accum,Condition
00:00:00,14 C,87�%,WSW,6�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:00:30,11 C,81�%,WSW,6�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:01:00,23 C,93�%,W,7�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:01:30,25 C,93�%,WSW,7�mph,0�mph,29.0�in,0.0�in,0.0�in,Mostly Cloudy
00:02:00,18 C,93�%,WSW,9�mph,0�mph,29.0�in,0.0�in,0.0�in,Light Rain Shower
00:02:30,10 C,93�%,W,9�mph,0�mph,29.0�in,0.0�in,0.0�in,Light Rain Shower
00:03:00,17 C,93�%,W,9�mph,0�mph,29.0�in,0.0�in,0.0�in,Mostly Cloudy
00:03:30,17 C,93�%,W,9�mph,0�mph,29.0�in,0.0�in,0.0�in,Light Rain Shower
00:04:00,23 C,100�%,W,8�mph,0�mph,29.0�in,0.0�in,0.0�in,Mostly Cloudy
00:04:30,26 C,100�%,W,10�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:05:00,20 C,100�%,WNW,13�mph,0�mph,29.1�in,0.0�in,0.0�in,Light Rain
00:05:30,21 C,93�%,W,10�mph,0�mph,29.1�in,0.0�in,0.0�in,Light Rain
00:06:00,25 C,93�%,W,9�mph,0�mph,29.1�in,0.0�in,0.0�in,Light Rain
00:06:30,25 C,100�%,W,13�mph,0�mph,29.1�in,0.0�in,0.0�in,Light Rain
00:07:00,27 C,93�%,W,14�mph,0�mph,29.1�in,0.0�in,0.0�in,Mostly Cloudy
00:07:30,27 C,93�%,W,13�mph,0�mph,29.1�in,0.0�in,0.0�in,Fair
00:08:00,14 C,93�%,W,15�mph,0�mph,29.1�in,0.0�in,0.0�in,Partly Cloudy
00:08:30,13 C,93�%,W,12�mph,0�mph,29.1�in,0.0�in,0.0�in,Fair
00:09:00,14 C,93�%,W,14�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair
00:09:30,28 C,93�%,W,16�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair
00:10:00,10 C,87�%,W,17�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair
00:10:30,18 C,81�%,W,20�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair
00:11:00,27 C,81�%,W,21�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair / Windy
00:11:30,15 C,76�%,W,22�mph,0�mph,29.2�in,0.0�in,0.0�in,Fair / Windy
00:12:00,28 C,76�%,W,23�mph,0�mph,29.3�in,0.0�in,0.0�in,Partly Cloudy / Windy
00:12:30,17 C,70�%,W,18�mph,0�mph,29.3�in,0.0�in,0.0�in,Fair
00:13:00,23 C,70�%,W,21�mph,0�mph,29.3�in,0.0�in,0.0�in,Partly Cloudy / Windy
00:13:30,23 C,66�%,W,21�mph,0�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy / Windy
00:14:00,18 C,61�%,W,23�mph,0�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy / Windy
00:14:30,14 C,66�%,W,22�mph,33�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy / Windy
00:15:00,28 C,66�%,WSW,18�mph,0�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy
00:15:30,18 C,66�%,WSW,16�mph,30�mph,29.3�in,0.0�in,0.0�in,Showers in the Vicinity
00:16:00,29 C,61�%,W,18�mph,32�mph,29.3�in,0.0�in,0.0�in,Mostly Cloudy
00:16:30,21 C,70�%,WSW,17�mph,29�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:17:00,12 C,70�%,WSW,16�mph,30�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:17:30,22 C,76�%,WSW,17�mph,0�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:18:00,22 C,87�%,WSW,18�mph,38�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:18:30,12 C,93�%,WSW,20�mph,35�mph,29.3�in,0.0�in,0.0�in,Light Rain
00:19:00,24 C,87�%,W,25�mph,0�mph,29.3�in,0.0�in,0.0�in,Light Rain / Windy
00:19:30,28 C,93�%,W,31�mph,43�mph,29.3�in,0.0�in,0.0�in,Light Rain / Windy
Reply
#6
you can attach the file, rather than posting contents.
To do so, rather than using Quick reply, click on New Reply and you will see the attach file button
Reply
#7
Lightbulb 
I tried to upload the file but actually I can't see the button. there is a button for link where I put the url for the file but still it doesn't load. if possible can you give me your email and i'll send it through that? or even if you can correct me that if I am loading the file in right place.
thanks.
Reply
#8
Need 5 > post to upload,so you can make one more post.
Or use a online file-share eg Filebin.
Reply
#9
THANKS A LOT.

BIN NUMBER
ixwd0vyvpeiqk7s4


THIS IS THE CSV FILE.
Reply
#10
The file is mess up(Unicode) from whatever source you get it from or saving done be you.
Try to work and save all with utf-8 as encoding.
Can fix data.
with open ('Data.txt', encoding='utf-8') as f,open('new_data.txt', 'w') as f_out:
    fixed_data = f.read().replace('�', '')
    f_out.write(fixed_data) 
Now is new data okay.
with open('new_data.txt') as f:
    for line in f:
        print(line.strip())
Output:
Time,Temperature,Humidity,Wind,Wind Speed,Wind Gust,Pressure,Precip.,Precip Accum,Condition 00:00:00,14 C,87%,WSW,6mph,0mph,29.1in,0.0in,0.0in,Mostly Cloudy 00:00:30,11 C,81%,WSW,6mph,0mph,29.1in,0.0in,0.0in,Mostly Cloudy 00:01:00,23 C,93%,W,7mph,0mph,29.1in,0.0in,0.0in,Mostly Cloudy 00:01:30,25 C,93%,WSW,7mph,0mph,29.0in,0.0in,0.0in,Mostly Cloudy 00:02:00,18 C,93%,WSW,9mph,0mph,29.0in,0.0in,0.0in,Light Rain Shower 00:02:30,10 C,93%,W,9mph,0mph,29.0in,0.0in,0.0in,Light Rain Shower 00:03:00,17 C,93%,W,9mph,0mph,29.0in,0.0in,0.0in,Mostly Cloudy 00:03:30,17 C,93%,W,9mph,0mph,29.0in,0.0in,0.0in,Light Rain Shower 00:04:00,23 C,100%,W,8mph,0mph,29.0in,0.0in,0.0in,Mostly Cloudy 00:04:30,26 C,100%,W,10mph,0mph,29.1in,0.0in,0.0in,Mostly Cloudy 00:05:00,20 C,100%,WNW,13mph,0mph,29.1in,0.0in,0.0in,Light Rain 00:05:30,21 C,93%,W,10mph,0mph,29.1in,0.0in,0.0in,Light Rain 00:06:00,25 C,93%,W,9mph,0mph,29.1in,0.0in,0.0in,Light Rain 00:06:30,25 C,100%,W,13mph,0mph,29.1in,0.0in,0.0in,Light Rain 00:07:00,27 C,93%,W,14mph,0mph,29.1in,0.0in,0.0in,Mostly Cloudy 00:07:30,27 C,93%,W,13mph,0mph,29.1in,0.0in,0.0in,Fair 00:08:00,14 C,93%,W,15mph,0mph,29.1in,0.0in,0.0in,Partly Cloudy 00:08:30,13 C,93%,W,12mph,0mph,29.1in,0.0in,0.0in,Fair 00:09:00,14 C,93%,W,14mph,0mph,29.2in,0.0in,0.0in,Fair 00:09:30,28 C,93%,W,16mph,0mph,29.2in,0.0in,0.0in,Fair 00:10:00,10 C,87%,W,17mph,0mph,29.2in,0.0in,0.0in,Fair 00:10:30,18 C,81%,W,20mph,0mph,29.2in,0.0in,0.0in,Fair 00:11:00,27 C,81%,W,21mph,0mph,29.2in,0.0in,0.0in,Fair / Windy 00:11:30,15 C,76%,W,22mph,0mph,29.2in,0.0in,0.0in,Fair / Windy 00:12:00,28 C,76%,W,23mph,0mph,29.3in,0.0in,0.0in,Partly Cloudy / Windy 00:12:30,17 C,70%,W,18mph,0mph,29.3in,0.0in,0.0in,Fair 00:13:00,23 C,70%,W,21mph,0mph,29.3in,0.0in,0.0in,Partly Cloudy / Windy 00:13:30,23 C,66%,W,21mph,0mph,29.3in,0.0in,0.0in,Mostly Cloudy / Windy 00:14:00,18 C,61%,W,23mph,0mph,29.3in,0.0in,0.0in,Mostly Cloudy / Windy 00:14:30,14 C,66%,W,22mph,33mph,29.3in,0.0in,0.0in,Mostly Cloudy / Windy 00:15:00,28 C,66%,WSW,18mph,0mph,29.3in,0.0in,0.0in,Mostly Cloudy 00:15:30,18 C,66%,WSW,16mph,30mph,29.3in,0.0in,0.0in,Showers in the Vicinity 00:16:00,29 C,61%,W,18mph,32mph,29.3in,0.0in,0.0in,Mostly Cloudy 00:16:30,21 C,70%,WSW,17mph,29mph,29.3in,0.0in,0.0in,Light Rain 00:17:00,12 C,70%,WSW,16mph,30mph,29.3in,0.0in,0.0in,Light Rain 00:17:30,22 C,76%,WSW,17mph,0mph,29.3in,0.0in,0.0in,Light Rain 00:18:00,22 C,87%,WSW,18mph,38mph,29.3in,0.0in,0.0in,Light Rain 00:18:30,12 C,93%,WSW,20mph,35mph,29.3in,0.0in,0.0in,Light Rain 00:19:00,24 C,87%,W,25mph,0mph,29.3in,0.0in,0.0in,Light Rain / Windy 00:19:30,28 C,93%,W,31mph,43mph,29.3in,0.0in,0.0in,Light Rain / Windy
So look at what i mention before,eg if print Temperature it will be in line[1].
with open('new_data.txt') as f:
    for line in f:
        #print(line.strip())
        line = line.split(',')
        print(line[1])
Output:
Temperature 14 C 11 C 23 C 25 C 18 C 10 C 17 C 17 C 23 C 26 C 20 C 21 C 25 C 25 C 27 C 27 C 14 C 13 C 14 C 28 C 10 C 18 C 27 C 15 C 28 C 17 C 23 C 23 C 18 C 14 C 28 C 18 C 29 C 21 C 12 C 22 C 22 C 12 C 24 C 28 C
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyspark creating temp files in /tmp folder aliyesami 1 4,821 Oct-16-2021, 05:15 PM
Last Post: aliyesami
  Help with storing temp data for each day then recording min/max in app. trthskr4 3 2,356 Sep-10-2021, 10:51 PM
Last Post: trthskr4
  How to save Matplot chart to temp file? Morkus 2 4,435 Jun-12-2021, 10:52 AM
Last Post: Morkus
  Get system info from PI (cpu load and temp) korenron 2 2,049 Aug-04-2019, 08:45 AM
Last Post: korenron
  Temp folder creation ste1605 7 5,182 Oct-03-2018, 10:39 AM
Last Post: buran
  If/ Or if/ temp controlled fan. clueless 5 3,988 Dec-25-2017, 03:21 AM
Last Post: DeaD_EyE
  Problem with remove Temp Files karlo_ds 1 3,119 Oct-26-2017, 11:42 PM
Last Post: wavic
  How to create a value system with a temp/humidity sensor? Tacoon 2 3,405 Feb-13-2017, 03:11 PM
Last Post: Tacoon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020