Enumerating the Writing of Different Lines in Txt file in Python - python

It seems that every time the string should add up the 4, 1, and 4, for column 1, the total result is just 4*3.
Could you help me put an enumeration-like function in here? (I am I very new beginner)
Thank you for anything!
import os
import platform
pathwindows = os.environ['USERPROFILE'] + r"\Documents\Your_Wordle_Results.txt"
pathmac = r'/Mac/Users/%USEPROFILE%/Documents/Your_Wordle_Results.txt'
isFileWindows = os.path.exists(pathwindows)
isFileMac = os.path.isfile(pathmac)
if isFileWindows == True:
outfile = open(pathwindows, 'r')
if isFileMac == True:
outfile = open(pathmac, 'r')
totalpoints1 = 0
totalpoints2 = 0
totalpoints3 = 0
totalpoints4 = 0
totalpoints5 = 0
with open(pathwindows, 'r') as fp:
lineofinterest = fp.readlines()[2:100]
stringlineofinterest = str(lineofinterest)
print(*lineofinterest)
for line in lineofinterest:
print(line.strip())
startline = 22
separation = 4
value1 = (stringlineofinterest[startline + separation * 0])
value2 = (stringlineofinterest[startline + separation * 1])
value3 = (stringlineofinterest[startline + separation * 2])
value4 = (stringlineofinterest[startline + separation * 3])
value5 = (stringlineofinterest[startline + separation * 4])
outfile.close
print(value1)
print(totalpoints1)
The text file is
Ben Jackson
1pt 2pt 3pt 4pt 5pt Total Results Will Be Shown Below
4 3 0 1 0 LOSS for audio in 7.28s
1 2 0 2 0 LOSS for audit in 6.18s
4 5 0 1 0 LOSS for audio in 7.28s
I expected for the 4 + 1 +4 to add up in the 1 pt column but rather the first "4" was multiplied 3 times meaning that the cycle that beings with "with open" did not enumerate through.

I'm going to answer this the best I can according to the post, there was problems with indentation, use of the correct variable to fetch the values (stringlineofinteres instead of line which is the one in the loop), your code, and finally no line to add vaalues to the totals:
import os
import platform
pathwindows = os.environ['USERPROFILE'] + r"\Documents\Your_Wordle_Results.txt"
pathmac = r'/Mac/Users/%USEPROFILE%/Documents/Your_Wordle_Results.txt'
pathwindows="enum.txt"
isFileWindows = os.path.exists(pathwindows)
isFileMac = os.path.isfile(pathmac)
if isFileWindows == True:
filepath=pathwindows
if isFileMac == True:
filepath=pathmac
totalpoints1 = 0
totalpoints2 = 0
totalpoints3 = 0
totalpoints4 = 0
totalpoints5 = 0
with open(filepath, 'r') as fp:
lineofinterest = fp.readlines()[2:100]
stringlineofinterest = str(lineofinterest)
print(*lineofinterest)
for line in lineofinterest:
print(line)
startline = 22
separation = 4
value1 = (line[startline + separation * 0])
totalpoints1 += int(value1)
value2 = (line[startline + separation * 1])
totalpoints2 += int(value2)
value3 = (line[startline + separation * 2])
totalpoints3 += int(value3)
value4 = (line[startline + separation * 3])
totalpoints4 += int(value4)
value5 = (line[startline + separation * 4])
totalpoints5 += int(value5)
# write the totals line here
with open(filepath,'a') as outfile:
outfile.write("totals xxxx")
print(totalpoints1, totalpoints2,totalpoints3,totalpoints4,totalpoints5)

Related

Python Last 6 Results, removing the last

I just can't get it done. Therefore I'll post the full code.
The .csv used is from http://www.football-data.co.uk/mmz4281/1415/E0.csv
Now when run, the variables home_team_a, home_team_d, away_team_a and away_team_d are based on all of the previous matches but I want them to be based always on the last 6 matches.
import csv, math, ast, numpy as np
def poisson(actual, mean):
return math.pow(mean, actual) * math.exp(-mean) / math.factorial(actual)
csvFile = '20152016.csv'
team_list = []
k = open('team_list.txt', 'w')
k.write("""{
""")
csvRead = csv.reader(open(csvFile))
next(csvRead)
for row in csvRead:
if row[2] not in team_list:
team_list.append(row[2])
if row[3] not in team_list:
team_list.append(row[3])
team_list.sort()
for team in team_list:
k.write(""" '%s': {'home_goals': 0, 'away_goals': 0, 'home_conceded': 0, 'away_conceded': 0, 'home_games': 0, 'away_games': 0, 'alpha_h': 0, 'beta_h': 0, 'alpha_a': 0, 'beta_a': 0},
""" % (team))
k.write("}")
k.close()
s = open('team_list.txt', 'r').read()
dict = ast.literal_eval(s)
GAMES_PLAYED = 0
WEEKS_WAIT = 4
TOTAL_VALUE = 0
csvRead = csv.reader(open(csvFile))
next(csvRead)
for game in csvRead:
home_team = game[2]
away_team = game[3]
home_goals = int(game[4])
away_goals = int(game[5])
home_win_prob = 0
draw_win_prob = 0
away_win_prob = 0
curr_home_goals = 0
curr_away_goals = 0
avg_home_goals = 1
avg_away_goals = 1
team_bet = ''
ev_bet = ''
# GETTING UPDATED VARIABLES
for key, value in dict.items():
curr_home_goals += dict[key]['home_goals']
curr_away_goals += dict[key]['away_goals']
if GAMES_PLAYED > (WEEKS_WAIT * 10):
avg_home_goals = curr_home_goals / (GAMES_PLAYED)
avg_away_goals = curr_away_goals / (GAMES_PLAYED)
# CALCULATING FACTORS
if GAMES_PLAYED > (WEEKS_WAIT * 10):
home_team_a = (dict[home_team]['alpha_h'] + dict[home_team]['alpha_a']) / 2
away_team_a = (dict[away_team]['alpha_h'] + dict[away_team]['alpha_a']) / 2
home_team_d = (dict[home_team]['beta_h'] + dict[home_team]['beta_a']) / 2
away_team_d = (dict[away_team]['beta_h'] + dict[away_team]['beta_a']) / 2
home_team_exp = avg_home_goals * home_team_a * away_team_d
away_team_exp = avg_away_goals * away_team_a * home_team_d
# RUNNING POISSON
l = open('poisson.txt', 'w')
for i in range(10):
for j in range(10):
prob = poisson(i, home_team_exp) * poisson(j, away_team_exp)
l.write("Prob%s%s = %s\n" % (i, j, prob))
l.close()
with open('poisson.txt') as f:
for line in f:
home_goals_m = int(line.split(' = ')[0][4])
away_goals_m = int(line.split(' = ')[0][5])
prob = float(line.split(' = ')[1])
if home_goals_m > away_goals_m:
home_win_prob += prob
elif home_goals_m == away_goals_m:
draw_win_prob += prob
elif home_goals_m < away_goals_m:
away_win_prob += prob
#CALCULATE VALUE
bet365odds_h, bet365odds_d, bet365odds_a = float(game[23]), float(game[24]), float(game[25])
ev_h = (home_win_prob * (bet365odds_h - 1)) - (1 - home_win_prob)
ev_d = (draw_win_prob * (bet365odds_d - 1)) - (1 - draw_win_prob)
ev_a = (away_win_prob * (bet365odds_a - 1)) - (1 - away_win_prob)
highestEV = max(ev_h, ev_d, ev_a)
if (ev_h == highestEV) and (ev_h > 0):
team_bet = home_team
ev_bet = ev_h
if home_goals > away_goals:
TOTAL_VALUE += (bet365odds_h - 1)
else:
TOTAL_VALUE -= 1
elif (ev_d == highestEV) and (ev_d > 0):
team_bet = 'Draw'
ev_bet = ev_d
if home_goals == away_goals:
TOTAL_VALUE += (bet365odds_d - 1)
else:
TOTAL_VALUE -= 1
elif (ev_a == highestEV) and (ev_a > 0):
team_bet = away_team
ev_bet = ev_a
if home_goals < away_goals:
TOTAL_VALUE += (bet365odds_a - 1)
else:
TOTAL_VALUE -= 1
if (team_bet != '') and (ev_bet != ''):
print ("Bet on '%s' (EV = %s)" % (team_bet, ev_bet))
print (TOTAL_VALUE)
# UPDATE VARIABLES AFTER MATCH HAS BEEN PLAYED
dict[home_team]['home_goals'] += home_goals
dict[home_team]['home_conceded'] += away_goals
dict[home_team]['home_games'] += 1
dict[away_team]['away_goals'] += away_goals
dict[away_team]['away_conceded'] += home_goals
dict[away_team]['away_games'] += 1
GAMES_PLAYED += 1
# CREATE FACTORS
if GAMES_PLAYED > (WEEKS_WAIT * 10):
for key, value in dict.items():
alpha_h = (dict[key]['home_goals'] / dict[key]['home_games']) / avg_home_goals
beta_h = (dict[key]['home_conceded'] / dict[key]['home_games']) / avg_away_goals
alpha_a = (dict[key]['away_goals'] / dict[key]['away_games']) / avg_away_goals
beta_a = (dict[key]['away_conceded'] / dict[key]['away_games']) / avg_home_goals
dict[key]['alpha_h'] = alpha_h
dict[key]['beta_h'] = beta_h
dict[key]['alpha_a'] = alpha_a
dict[key]['beta_a'] = beta_a
Use a deque to keep the 6 most recent items in memory; adding a new record will "push out" the oldest one.
import collections
import itertools
import csv
with open("foo.csv") as fh:
# Skip the first 44 rows
csv_read = islice(csv.reader(fh), 44, None)
# Initialize the deque with the next 6 rows
d = collections.deque(islice(csv_read, 6), 6)
for record in csv_read:
d.append(record)
print(list(d)) # Rows 46-51, then 47-52, then 48-53, etc
Because you set the maximum length of the deque to 6, each append to a "full" deque pushes out the older one. On the first iteration, d.append pushes out row 45 and adds row 51. On the next iteration, adding row 52 pushes out row 46, etc.
In general, a deque is a data structure that is like a combination of a queue and a stack; you can add or remove items to either end efficiently, but accessing an arbitrary item or modifying the "middle" is slow. Here, we're taking advantage of the fact that appending to a full deque causes an implicit removal from the opposite end.
How about:
if seen_records == 200:
recs = list(csvRead)[seen_records - 6:seen_records + 1]
You can do something like this....
previous_index = 0
previous_max = 6 # max number of previous numbers to remember
previous = [None for _ in range(previous_max)]
csvFile = 'X.csv'
seen_records = 0
csvRead = csv.reader(open(csvFile))
# Enumerate over the records to keep track of the index of each one
for i, records in enumerate(csvRead):
if (i > 50):
seen_records =+ 1
if previous_index == previous_max:
previous_index = 0 # Reset to the beginning when we reach the end
# Store the record and increment the index to the next location
previous[previous_index] = record
previous_index += 1
This creates a very basic array of length previous_max and just stores the oldest data at index 0 and newest at previous_max -1.

entire program not executing

I have a program that interfaces with another program to process data. The program I have written has three main parts (separated by lines of ### below). When I run the entire program, it does not work (specifically the second subprocess call doesn't execute and produce a summary file for which the third part of the program is responsible for dealing with). However, whenever I break them into three separate programs and run them in the terminal, everything is fine. Any tips or suggestions as to how to get this to run as one program?
#### part 1:
import glob
import subprocess
import os
import datetime
import matplotlib.pyplot as plt
import csv
import re
import ntpath
x = open('data.txt', 'w')
m = open('graphing_data.txt', 'w')
ckopuspath= '/Volumes/DAVIS/sfit-ckopus/ckopus'
command_init = 'sfit4Layer0.py -bv5 -fh'
subprocess.call(command_init.split(), shell=False)
for line in open('sfit4.ctl', 'r'): # write in later specific directory line
p = line.strip()
if p.startswith('band.1.nu_start'):
a,b = p.split('=')
b = float(b)
b = "{0:.3f}".format(b)
lowwav = b
lowwav = float(lowwav)
if p.startswith('band.1.nu_stop'):
a,b = p.split('=')
b = float(b)
b = "{0:.3f}".format(b)
highwav = b
highwav = float(highwav)
with open('/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/info.txt', 'rt') as infofile: # the info.txt file created by CALPY
for count, line in enumerate(infofile):
with open('t15asc.4', 'w') as t:
lat = re.search('Latitude of location:\s*([^;]+)', line, re.IGNORECASE).group(0)
lat = lat.split()
lat = lat[3]
lat = float(lat)
lon = re.search('Longitude of location:\s*([^;]+)', line, re.IGNORECASE).group(0)
lon = lon.split()
lon = lon[3]
lon = float(lon)
date = re.search('Time of measurement \(UTC\): ([^;]+)', line).group(0)
date = date.split()
yeardate = date[4]
yeardate = yeardate.split('-')
year = int(yeardate[0])
month = int(yeardate[1])
day = int(yeardate[2])
time = date[5]
time = time.split(':')
hour = int(time[0])
minute = int(time[1])
second = time[2]
second = second.split('.')
second = int(second[0])
dur = re.search('Duration of measurement \[s\]: ([^;]+)', line).group(0)
dur = dur.split()
dur = float(dur[4])
numpoints = re.search('Number of values of one scan:\s*([^;]+)', line, re.IGNORECASE).group(0)
numpoints = numpoints.split()
numpoints = float(numpoints[6])
fov = re.search('semi FOV \[rad\] :\s*([^;]+)', line, re.IGNORECASE).group(0)
fov = fov.split()
fov = fov[3]
fov = float(fov[1:])
sza = re.search('sun Azimuth \[deg\]:\s*([^;]+)', line, re.IGNORECASE).group(0)
sza = sza.split()
sza = float(sza[3])
snr = 0.0000
roe = 6396.2
res = 0.5000
calpy_path = '/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/140803/*' # the CALPY output files!
files1 = glob.glob(calpy_path)
with open(files1[count], 'r') as g:
ints = []
ints_count = 0
for line in g:
wave_no, intensity = [float(item) for item in line.split()]
if lowwav <= wave_no <= highwav:
ints.append(str(intensity))
ints_count = ints_count + 1
spacebw = (highwav - lowwav)/ ints_count
d = datetime.datetime(year, month, day, hour, minute, second)
t.write('{:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}'.format(sza,roe,lat,lon,snr)) # line 1
t.write("\n")
t.write('{:>10d}{:>5d}{:>5d}{:>5d}{:>5d}{:>5d}'.format(year,month,day,hour,minute,second)) # line 2
t.write("\n")
t.write( ('{:%Y/%m/%d %H:%M:%S}'.format(d)) + "UT Solar Azimuth:" + ('{:>6.3f}'.format(sza)) + " Resolution:" + ('{:>6.4f}'.format(res)) + " Duration:" + ('{:>6.2f}'.format(dur))) # line 3
t.write("\n")
t.write('{:>22.12f}{:>22.12f}{:>22.12e}{:>15d}'.format(lowwav,highwav,spacebw,ints_count)) # line 4
t.write("\n")
t.write('\n'.join(map(str, ints)) + '\n')
######################################## part 2:
command_two = 'sfit4Layer0.py -bv5 -fs'
subprocess.call(command_two.split(), shell=False) #writes the summary file
######################################## part 3
infile = '/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/summary'
lines = open(infile, 'r').readlines()
line_number = 0
line4= lines[line_number + 3]
variables = line4.split(" ")
sample = variables[1]
time = variables[2]
Z = variables[3]
A = variables[4]
D = variables[5]
R = variables[6]
P = variables[7]
V = variables[8]
E = variables[9]
x.write('{0} {1} {2} {3} '.format(sample, time, Z, A))
time = time[:-2]
numofgas = lines[line_number + 5]
numofgas = int(numofgas.strip())
x.write(str(numofgas))
count2 = 0
while count2 < numofgas:
gasinfo = lines[line_number + 7 + count2]
data = gasinfo.split()
gasnum = data[0]
gasname = data[1]
IFPRF = data[2]
apprcol = data[3]
retcol = data[4]
x.write(' {0} {1}'.format(gasname, retcol))
m.write('{0},{1}\n'.format(time, retcol))
count2 = count2 + 1
line17 = lines[line_number + 10 + count2]
print(numofgas)
params = line17.split()
bandstart = params[1]
bandstop = params[2]
space = params[3]
nptsb = params[4]
pmax = params[5]
fovdia = params[6]
meansnr = params[7]
x.write(' {0} {1} {2} {3} '.format(bandstart, bandstop, nptsb, meansnr))
line21 = lines[line_number + 14 + count2]
info = line21.split()
fitrms = info[0]
chi2 = info[1]
dofsall = info[2]
dofstrg = info[3]
iter = info[4]
maxxiter = info[5]
x.write('{0} {1} {2} {3} {4}'.format(fitrms, chi2, dofsall, dofstrg, iter))
x.write('\n')
x.close()
m.close()

Caesar's Cipher decoding program in python

Hey guys I'm having issue with my program that does the following:
1.) takes in one file that generates the relative frequency of letters that will be assumed to be average.
2.) takes a second file that contains the coded message.
3.) tests each possible rotation.
4.) creates a new txt file containing the decoded message as the output
here is my code:
# This is the module that we import to check if a file name exists
import os
# This is the dictionary used later to store individual letter counts, which
# allows us to calculate the relative frequency of each letter
d1 = { }
d1['a'] = 0
d1['b'] = 0
d1['c'] = 0
d1['d'] = 0
d1['e'] = 0
d1['f'] = 0
d1['g'] = 0
d1['h'] = 0
d1['i'] = 0
d1['j'] = 0
d1['k'] = 0
d1['l'] = 0
d1['m'] = 0
d1['n'] = 0
d1['o'] = 0
d1['p'] = 0
d1['q'] = 0
d1['r'] = 0
d1['s'] = 0
d1['t'] = 0
d1['u'] = 0
d1['v'] = 0
d1['w'] = 0
d1['x'] = 0
d1['y'] = 0
d1['z'] = 0
# This asks for the user to enter a file to parse
filename = raw_input("Path to a file to parse: ")
# This is the basic if/else statement that keeps track of each letter counter
# in the dictionary above if the file exists, and displays and error message
# and quits if it doesn't exist.
if os.path.exists(filename):
f = open(filename, 'r')
counter = 0
for line in f:
for j in line:
if j.isalpha():
counter += 1
d1[j.lower()] += 1
f.close()
else:
print "Error: cannot find",filename
quit()
# This is the definition that give us the relative frequency by dividing the
# dictionary key value for each character by the total number of characters
def relfreq(character):
return d1[character] / float(counter)
### This is the end of the previous module's code ###
# This code creates a list of the average frequencies of letter
lof1 = [relfreq('a'), relfreq('b'), relfreq('c'), relfreq('d'), relfreq('e'),
relfreq('f'), relfreq('g'), relfreq('h'), relfreq('i'), relfreq('j'),
relfreq('k'), relfreq('l'), relfreq('m'), relfreq('n'), relfreq('o'),
relfreq('p'), relfreq('q'), relfreq('r'), relfreq('s'), relfreq('t'),
relfreq('u'), relfreq('v'), relfreq('w'), relfreq('x'), relfreq('y'),
relfreq('z')]
# This code finds the relative frequency of the coded message
d2 = { }
d2['a'] = 0
d2['b'] = 0
d2['c'] = 0
d2['d'] = 0
d2['e'] = 0
d2['f'] = 0
d2['g'] = 0
d2['h'] = 0
d2['i'] = 0
d2['j'] = 0
d2['k'] = 0
d2['l'] = 0
d2['m'] = 0
d2['n'] = 0
d2['o'] = 0
d2['p'] = 0
d2['q'] = 0
d2['r'] = 0
d2['s'] = 0
d2['t'] = 0
d2['u'] = 0
d2['v'] = 0
d2['w'] = 0
d2['x'] = 0
d2['y'] = 0
d2['z'] = 0
filename2 = raw_input("Path to encoded message: ")
if os.path.exists(filename2):
f2 = open(filename2, 'r')
counter2 = 0
for line2 in f2:
for j2 in line2:
if j2.isalpha():
counter2 += 1
d2[j2.lower()] += 1
f2.close()
else:
print "Error: cannot find",filename2
quit()
def relfreq2(character):
return d2[character] / float(counter2)
# This code creates a list of relative frequencies of the coded message
lof2 = [relfreq2('a'), relfreq2('b'), relfreq2('c'), relfreq2('d'), relfreq2('e'),
relfreq2('f'), relfreq2('g'), relfreq2('h'), relfreq2('i'), relfreq2('j'),
relfreq2('k'), relfreq2('l'), relfreq2('m'), relfreq2('n'), relfreq2('o'),
relfreq2('p'), relfreq2('q'), relfreq2('r'), relfreq2('s'), relfreq2('t'),
relfreq2('u'), relfreq2('v'), relfreq2('w'), relfreq2('x'), relfreq2('y'),
relfreq2('z')]
##### Not sure if this is correct #####
scores = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
d3 = { }
d3['a'] = 0
d3['b'] = 1
d3['c'] = 2
d3['d'] = 3
d3['e'] = 4
d3['f'] = 5
d3['g'] = 6
d3['h'] = 7
d3['i'] = 8
d3['j'] = 9
d3['k'] = 10
d3['l'] = 11
d3['m'] = 12
d3['n'] = 13
d3['o'] = 14
d3['p'] = 15
d3['q'] = 16
d3['r'] = 17
d3['s'] = 18
d3['t'] = 19
d3['u'] = 20
d3['v'] = 21
d3['w'] = 22
d3['x'] = 23
d3['y'] = 24
d3['z'] = 25
def get_scores():
ii = 0
jj = 0
for ii in range(25):
for jj in range(26):
if ii + jj <26:
scores[jj] += lof1[jj] * lof2[jj + ii]
jj += 1
else:
scores[jj] += lof1[jj] * lof2[jj + ii - 26]
jj += 1
ii += 1
# This is the code that determines which match is the best match
get_scores()
rotationscore = max(scores)
rotations_ttr = scores.index(rotationscore)
print "Shift",rotations_ttr,"letters to the right"
loa = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','']
# This code 'decodes' the coded message
if os.path.exists(filename):
f3 = open(filename2, 'r')
counter3 = 0
for line3 in f3:
for j3 in line3:
if j2.isalpha():
counter3 += 1
j3 = d3[j3.lower()]
line3.replace(loa[int(j3)], loa[int(j3 + rotations_ttr)])
print
f.close()
I currently get the error:
Path to a file to parse: ./Phoenix.py Traceback (most recent call
last): File "/Users/atloftus/Desktop/Lecture Code/Labs/decipher.py",
line 85, in
lof1 = [relfreq('a'), relfreq('b'), relfreq('c'), relfreq('d'), relfreq('e'), File "/Users/atloftus/Desktop/Lecture
Code/Labs/decipher.py", line 79, in relfreq
return d1[character] / float(counter) ZeroDivisionError: float division by zero
How do I get rid of that error? It wasn't there earlier and now I don't know what I changed to cause it. Thanks

Unexpected EOF while parsing; trying calculate mean/max/min of each line reading in

the data looks like this:
line = infile.readlines()
line
['56047257 16 17 19 16 12 15 12 20 58 123 59\n',
'97231934 18 16 13 19 16 12 13 18 72 101 55\n',
....same]
I want to get the average of the 2 to 9 column and get max and min of 2 to 12 column by using the loop below, but it keep giving me an error:
File "<string>", line unknown
^
SyntaxError: unexpected EOF while parsing
This is what my code looks like :
def main():
#read data in
infile = open('data.txt', 'r')
sun = 0.0
count = 0
line = infile.readline()
while line != "":
ID = line.split(" ")
min_val = float('inf')
max_val = -float('inf')
count_min = 0
count_max = 0
for xStr in line.split(' ')[1:9]:
sun = sun + eval(xStr)
count = count + 1
avg = round(sun / count, 2)
val = eval(xStr)
if val < min_val:
min_val = val
count_min = 1
elif val == min_val:
count_min += 1
if val > max_val:
max_val = val
count_max = 1
elif val == max_val:
count_max += 1
line = infile.readline()
print (ID, ' ',avg,' ',min_val,' ',max_val)
main()
Take note of the issues raised in the comments section of your post, but with that said, this is a much easier way of getting your desired output:
def main():
#read data in
infile = open('data.txt', 'r')
average = max_val = min_val = 0.0
count1=count2 = 0
line = infile.readlines()
for x in [x.strip().split() for x in line[:9]]:
x = ID =map(int, x)
average = (average + (sum(x)/len(x)))/len(x)
print average
for x in [x.strip().split() for x in line[:12]]:
x = map(int, x)
val=max(x)
if count1 !=0 and val>max_val:
max_val = val
val=min(x)
if count2 !=0 and val<min_val:
min_val = val
if count1==0:
max_val=max(x)
min_val=min(x)
count1=count2=1
print (ID, ' ',average,' ',min_val,' ',max_val)
main()
Note: You should try not assign infinty to variables. There are usually always better alternatives

list filtering in python

my program will import a file created by user having maximum of 15 lines of text in it(examples below)
#ID #value1 # value2
445 4 9000
787 2 4525
405 4 3352
415 1 2854
455 2 5500
r
The program then filter all the lines that have #value 1 larger than 2 and #value 2 larger than 3000 and print out their #ID, ignoring the rest.
here's what i have done so far
filename = ('input.txt')
infile = open(filename, 'r')
list_id = []
list_value1 = []
list_value2 = []
masterlist = []
for line in infile:
id, value1, value2 = line.split()
list_id.append(id)
list_value1.append(value1)
list_value2.append(value2)
masterlist.append(list_id)
masterlist.append(list_value1)
masterlist.append(list_value2)
#filtering part
sort = [i for i in masterlist[1] if i > 2 ] and [p for p in masterlist[2] if p > 3000]
#do something here to print out the ID of the filtered lines
Using your code as a starting point:
filename = ('input.txt')
infile = open(filename, 'r')
ids = []
for line in infile:
id, value1, value2 = line.split()
if int(value1) > 2 and int(value2) > 3000:
ids.append(id)
Put in exception handling for non-integer values as needed.

Categories

Resources