Green Building Bible, Fourth Edition |
![]() |
These two books are the perfect starting place to help you get to grips with one of the most vitally important aspects of our society - our homes and living environment. PLEASE NOTE: A download link for Volume 1 will be sent to you by email and Volume 2 will be sent to you by post as a book. |
Vanilla 1.0.3 is a product of Lussumo. More Information: Documentation, Community Support.
# Continuously extract data
while(True):
# Run the DHT program to get the humidity and temperature readings!
output = subprocess.check_output(["./Adafruit_DHT","11", "4"]);
import csv
help(csv)
Description:,"Celsius-based heating degree days for a base temperature of 12.0C"
Source:,"www.degreedays.net (using temperature data from www.wunderground.com)"
Accuracy:,"Estimates were made to account for missing data: the ""% Estimated"" column shows how much each figure was affected (0% is best, 100% is worst)"
Station:,"Wick (3.09W,58.46N)"
Station ID:,"EGPC"
Date,HDD,% Estimated
2008-12-01,9.1,0
2008-12-02,10.4,0
2008-12-03,11.3,0
2008-12-04,7.9,0
2008-12-05,8,0
2008-12-06,9.3,0
#!/usr/bin/python3
for line in open('EGPC_HDD_12.0C.csv').readlines():
line = line.strip()
if (len(line) >= 1) and (line[0] in '12'):
date, hdd, estimate = line.split(',')
hdd = float(hdd)
y, m, d = map(int, date.split('-'))
print(y, m, d, hdd)
2008 12 1 9.1
2008 12 2 10.4
2008 12 3 11.3
2008 12 4 7.9
2008 12 5 8.0
2008 12 6 9.3
Posted By: SteamyTeaGoing to see if I can understand this bit of code, I shall put my comments before the code:Not quite. Actually readlines reads all of the lines from the file and puts them in a list. If you had a *really* big file (bigger than the GB or so of memory you've probably got to hand) then a different approach would be needed. The for loop then loops over the whole of that list.
#opens the csv file so that all lines can be read. if I put a number in the () after the readlines would that limit the lines it reads, either to or from?
for line in open('EGPC_HDD_12.0C.csv').readlines():
#creates a working file somewhere in memory that is used and manipulated, in this case stripping out unwanted stuffYes. Removes whitespace from the beginning and end of the line. In particular, removes the newline character which readlines leaves on the end of most lines.
line = line.strip()
#only works on lines that have more than 1 character in them, but don't understand what the (line[0] in '12') does, what does the [0] and '12' do?A string is an indexable list of characters. line[0] selects the first character in the line in the same way an index like this would select the first element in a list or tuple. The "in '12'" part tests to see if the first character in the line is in the string '12', ie, if it's a '1' or a '2' so a line from the second or third millenniums AD.
if (len(line) >= 1) and (line[0] in '12'):
#creates columns that were separated by a comma and gives them namesYes. Also checks there are exactly three columns - if there were more or fewer it would blow up.
date, hdd, estimate = line.split(',')
#makes the data in the hdd column have a floating decimal pointNot quite. The date, hdd and estimate column values split out of the line are strings of characters. Using the float type as a function like this causes it to convert that string to a number.
hdd = float(hdd)
#all about the date format, I assume that map is some clever thing that puts parts of the date that are separated by a - into a form that does not have the -, but a space instead and only prints the integer. So if my date and time info is in the form 27/07/2014 01:10 then I will have to split it into two, the date and the time, then recombine the date part and the time part.date.split('-') returns a list of strings: the three separate fields which make up the date. Map is a function which takes a function and a list (or other iterable) and applies the function to each element of the list making up a new list of the results. As with float above, int called as a function takes a string and returns an integer value parsed from it.
y, m, d = map(int, date.split('-'))
#prints out the result with a blank space between each element.Yep.
print(y, m, d, hdd)
Posted By: Ed Daviesfor line in open('EGPC_HDD_12.0C.csv').readlines()[10:12]:
bearing in mind that the first line of the file is line zero and that array slices go from the first mentioned index up to but not including the second.
Posted By: SteamyTeaLinux file system is just horribleOh no, can't let that pass. Unix file systems are a thing of beauty. Microsoft/DOS files systems are just horrible
Posted By: borpinOh no, can't let that pass. Unix file systems are a thing of beauty. Microsoft/DOS files systems are just horriblehttp:///forum114/extensions/Vanillacons/smilies/standard/bigsmile.gif" alt="
" title="
" >
firefox
./script_name