#file_data = data #[ aNumber.strip() for aRow in data for aNumber in aRow ] file_data = 'age_from, age_to, average_intake, recommended_intake_low, recommended_intake_high\n' for aRow in data: element_count = len(aRow) for i, aNumber in enumerate(aRow): if (element_count == 4) and (i==1) : file_data += '120,'
file_data += aNumber
if (i < len(aRow) - 1): file_data += ','
file_data += '\n'
#file_data = str(file_data) print(file_data) file.write(file_data) file.close()#print('Select a health measure/aspect to visualize\n') # create the interactive interface def f(diet_file): return diet_file
print('Select a measure:') diet_file = interactive(f, diet_file = diet_files); display(diet_file)Select a measure:
# open the file for reading #path = 'dietfiles/vegetable_intake.txt' if os.path.exists(path): # print('debug: exists') file = open(path,'r') file_f = open(path_f,'r') else: print('Fatal error: file does not exist') exit(1)
# list to store the found dates, hashes, and events (named accordingly) # interpretation of the problem: three diferent output date_list = [] hash_list = [] event_list = []
for line in file: if line !='': # find date # assumption: dates are in the correct format and no wrong dates are there in the file # assumption: dates are in a consistent format such as yyyy-mm-dd # times are ignored assumed times are not part of date as output # year: 4 digit, where any digit can be 0 to 9. 9999 is the maximum year allowed # month: always 2 digits assumed. first digit can be between 0 and 1 inclusive #day: 0 to 31. first digit: 0 to 3, 2nd digit: 0 to 9 date_part= re.findall('[0-9]{4}-[01][1-2]-[0-3][0-9]', line.split()[0])
# append the date to the list date_list.append(date_part)
# hash part hash_part=line.split()[1] hash = re.findall('[a-zA-Z0-9]+', hash_part) hash_list.append(hash)
All events found []# 1.3 method 2 : output line by line import os.path import re
# open the file for reading path = 'logs.txt' if os.path.exists(path): # print('debug: exists') file = open('logs.txt','r') else: print('Fatal error: file does not exist') exit(1)
# list to store the found dates, hashes, and events (named accordingly) # interpretation of the problem: three diferent output date_list = [] hash_list = [] event_list = []
line_by_line = [] for line in file: if line !='': # find date # assumption: dates are in the correct format and no wrong dates are there in the file # assumption: dates are in a consistent format such as yyyy-mm-dd # times are ignored assumed times are not part of date as output # year: 4 digit, where any digit can be 0 to 9. 9999 is the maximum year allowed # month: always 2 digits assumed. first digit can be between 0 and 1 inclusive #day: 0 to 31. first digit: 0 to 3, 2nd digit: 0 to 9 date_part = re.findall('[0-9]{4}-[01][1-2]-[0-3][0-9]', line.split()[0])
# append the date to the list date_list.append(date_part)
All proceeds from Medium will go to Justetc Social Services ( non-profit). Justetc Social Services provides services in the Training and Education Areas.