I'm trying to write certain measurements to an output file in python3, but the output file only reflects the first 10 lines
I'm using the following code to write to the file:
f = open("measurements.txt", "w")
for infile in glob.glob("./WAVs/*"):
#do some stuff with the input file
f.write(outfile.removesuffix(".wav") + "\t" + str(oldSIL) + "\t" +
str(oldSPL) + "\t" + str(oldLoud) + "\t" + str(newLoud) + "\t"+
infile.removesuffix(".wav") + "\n")
f.close()
Looking at measurements.txt I find that only the first 10 lines of the expected output have been written.
If I try to print the same lines instead of writing to a file using
print(outfile.removesuffix(".wav") + "\t" + str(oldSIL) + "\t" + str("oldSPL") + "\t" + str(oldLoud) + "\t" + str(newLoud) + "\t"+ infile.removesuffix(".wav") + "\n")
It correctly prints every single line up to the final index. I'm a little lost as to why this might be the case.
in the below posted code, the first nested for loops displays the logs or the print statemnt as expected. but the latter nested for loops which has k and l as indces never displys the logs or the print statement within it.
please let me know why the print statement
print(str(x) + ",,,,,,,,,,,,,,,,,,," + str(y))
never gets displayed despite the polygonCoordinatesInEPSG25832 contains values
python code:
for feature in featuresArray:
polygonCoordinatesInEPSG4326.append(WebServices.fetchCoordinateForForFeature(feature))
for i in range(len(polygonCoordinatesInEPSG4326)):
for j in range(len(polygonCoordinatesInEPSG4326[i])):
lon = polygonCoordinatesInEPSG4326[i][j][0]
lat = polygonCoordinatesInEPSG4326[i][j][1]
x, y = transform(inputProj, outputProj, lon, lat)
xy.append([x,y])
print ("lon:" + str(lon) + "," + "lat:" + str(lat) + "<=>" + "x:" + str(x) + "," + "y:" , str(y))
print(str(x) + "," + str(y))
print("xy[%d]: %s"%(len(xy)-1,str(xy[len(xy)-1])))
print("\n")
print("len(xy): %d"%(len(xy)))
polygonCoordinatesInEPSG25832.append(xy)
print("len(polygonCoordinatesInEPSG25832[%d]: %d"%(i,len(polygonCoordinatesInEPSG25832[i])))
xy.clear()
print("len(polygonCoordinatesInEPSG25832 = %d" %(len(polygonCoordinatesInEPSG25832)))
for k in range(len(polygonCoordinatesInEPSG25832)):
for l in range(len(polygonCoordinatesInEPSG25832[k])):
x = polygonCoordinatesInEPSG25832[k][l][0]
y = polygonCoordinatesInEPSG25832[k][l][1]
print(str(x) + ",,,,,,,,,,,,,,,,,,," + str(y))
polygonCoordinatesInEPSG25832 contain values but polygonCoordinatesInEPSG25832[k] don't.
You append it with xy but you didn't unlinked it so when you call xy.clear() it become empty. Try deep copy it instead.
I have two data frame like below one is df and another one is anomalies:-
d = {'10028': [0], '1058': [25], '20120': [29], '20121': [22],'20122': [0], '20123': [0], '5043': [0], '5046': [0]}
df = pd.DataFrame(data=d)
Basically anomalies in a mirror copy of df just in anomalies the value will be 0 or 1 which indicates anomalies where value is 1 and non-anomaly where value is 0
d = {'10028': [0], '1058': [1], '20120': [1], '20121': [0],'20122': [0], '20123': [0], '5043': [0], '5046': [0]}
anomalies = pd.DataFrame(data=d)
and I am converting that into a specific format with the below code:-
details = (
'\n' + 'Metric Name' + '\t' + 'Count' + '\t' + 'Anomaly' +
'\n' + '10028:' + '\t' + str(df.tail(1)['10028'][0]) + '\t' + str(anomalies['10028'][0]) +
'\n' + '1058:' + '\t' + '\t' + str(df.tail(1)['1058'][0]) + '\t' + str(anomalies['1058'][0]) +
'\n' + '20120:' + '\t' + str(df.tail(1)['20120'][0]) + '\t' + str(anomalies['20120'][0]) +
'\n' + '20121:' + '\t' + str(round(df.tail(1)['20121'][0], 2)) + '\t' + str(anomalies['20121'][0]) +
'\n' + '20122:' + '\t' + str(round(df.tail(1)['20122'][0], 2)) + '\t' + str(anomalies['20122'][0]) +
'\n' + '20123:' + '\t' + str(round(df.tail(1)['20123'][0], 3)) + '\t' + str(anomalies['20123'][0]) +
'\n' + '5043:' + '\t' + str(round(df.tail(1)['5043'][0], 3)) + '\t' + str(anomalies['5043'][0]) +
'\n' + '5046:' + '\t' + str(round(df.tail(1)['5046'][0], 3)) + '\t' + str(anomalies['5046'][0]) +
'\n\n' + 'message:' + '\t' +
'Something wrong with the platform as there is a spike in [values where anomalies == 1].'
)
The problem is the column values are changing always in every run I mean like in this run its '10028', '1058', '20120', '20121', '20122', '20123', '5043', '5046' but maybe in next run it will be '10029', '1038', '20121', '20122', '20123', '5083', '5946'
How I can create the details dynamically depending on what columns are present in the data frame as I don't want to hard code and in the message i want to pass the name of columns whose value is 1.
The value of columns will always be either 1 or 0.
Try this:
# first part of the string
s = '\n' + 'Metric Name' + '\t' + 'Count' + '\t' + 'Anomaly'
# dynamically add the data
for idx, val in df.iloc[-1].iteritems():
s += f'\n{idx}\t{val}\t{anomalies[idx][0]}'
# for Python 3.5 and below, use this
# s += '\n{}\t{}\t{}'.format(idx, val, anomalies[idx][0])
# last part
s += ('\n\n' + 'message:' + '\t' +
'Something wrong with the platform as there is a spike in [values where anomalies == 1].'
)
There are probably many more straightforward ways of achieving my aim here, but as a Python newbie, I'd appreciate it if we could just concentrate on the code I'm using.
I'm attempting to write out a list of commands for a graphics program (G'MIC) and output those to a command prompt (although not directly) using Python 3.6.4 (OS: Windows 10 Pro), I need to write many lines of code but each line has to have different sets of numbers (integers and floats).. for example in the code I'm attempting to use right now the syntax is like this:
gmic v -99 input_.png fx_pastell
0.5,1,0,10,40,633,11,95,1,1,0.291651967558584,0,0,8,150,1,81,1,0,0,0 -o
out_001.png
^ all the variables denote parameters within the particular script (in this case Pastell effect).
The code I wrote relies on the Random module to get certain numbers within a range for each parameter that I want to change.
My stumbling block is to get the script to output DIFFERENT random numbers each time it prints a line. Here is my code (it's awful, I know..):
import random
a = random.randint (3,13)
b = random.randint (1,68)
c = random.randint (1,682)
d = random.randint (2,12)
e = random.randint (1,109)
g = random.randint (1,8)
h = random.uniform (0, 1)
k = random.randint (1,11)
l = random.randint (1,201)
n = random.randint (1,300)
o = random.randint (1,4)
dataFile = open("gmic1.txt", "w")
for line in range(120):
dataFile.write("gmic v -99 input_.png fx_pastell 0.5,1,0" + "," + str(a)
+ "," + str(b) + "," + str(c) + "," +str(d) + "," + str(e) + ",1," + str(g)
+ "," + str(h) + ",0,0," + str(k) + "," + str(l) + ",1," + str(n) + "," +
str(o) +"," + "0,0,0 -o out_%04d.png \n" % line)
dataFile.close()
The output is like this:
gmic v -99 input_.png fx_pastell
0.5,1,0,12,2,521,12,85,1,7,0.04003331068764937,0,0,8,17,1,297,2,0,0,0 -o
out_0000.png
gmic v -99 input_.png fx_pastell
0.5,1,0,12,2,521,12,85,1,7,0.04003331068764937,0,0,8,17,1,297,2,0,0,0 -o
out_0001.png
.. etc...
no errors as such, but not outputting a different set of numbers each time as I'd hoped.
The first input image name variable I can change with Notepad++'s Column Editor, so that takes care of that, I only need to know how to make each line of code differ.
Any help greatly appreciated!
You should call rand function on every iteration
from random import randint, uniform
dataFile = open("gmic1.txt", "w")
for line in range(120):
dataFile.write("gmic v -99 input_.png fx_pastell 0.5,1,0"
+ "," + str(randint(3, 13))
+ "," + str(randint(1, 68))
+ "," + str(randint(1, 682))
+ "," + str(randint(2, 12))
+ "," + str(randint(1, 109))
+ ",1," + str(randint(1, 8))
+ "," + str(uniform(0, 1))
+ ",0,0," + str(randint(1, 11))
+ "," + str(randint(1, 201))
+ ",1," + str(randint(1, 300))
+ "," + str(randint(1, 4))
+ "," + "0,0,0 -o out_%04d.png \n" % line)
dataFile.close()
so the below code is supposed to take the first element in the resulting tuple of x and convert it to a string to be used. However, when executing the last line it tells me it can't convert from tuple to str.
for x in filelink:
print(x[0])
item = str(x[0])
oldpath = root.wgetdir + "\\" + root.website.get() + "\\" + item
print(oldpath)
if os.path.exists(oldpath): shutil.copy(root.wgetdir + "\\" + root.website.get() + "\\" + x, keyworddir + "\\" + item)
This part:
root.wgetdir + "\\" + root.website.get() + "\\" + x
right here ^
is using the tuple instead of item.