Writing measurements to a file only writes the first 10 lines - python

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.

Related

Error upon inserting excel formula from Python to excel tab

So I was asked to insert this formula to each tab in excel. Problem is caused by "=" because if I remove "=" from below code it works just fine.
I use library openpyxl
page['%s%s' % (slownik['transport cost per m3'], str(k))] = "=JEŻELI.BĄŁD(" + slownik['price per truck'] + str(k) + "/" + \
slownik['m3 per truck'] + ")" + str(k)
I got an error and excel breaks.
This below works just fine.
page['%s%s' % (slownik['transport cost per m3'], str(k))] = "JEŻELI.BĄŁD(" + slownik['price per truck'] + str(k) + "/" + \
slownik['m3 per truck'] + ")" + str(k)
Okay so. First of all formula should be done in english. Also In formula I shouldn't use
";"
I should use:","
So in the end formula inside python looks like this:
page['%s%s' % (slownik['transport cost per m3'], str(k))] = "=IFERROR(" + slownik['price per truck'] + str(k) + "/" + slownik['m3 per truck'] + str(k) + ",0)"

Dynamically create string from pandas column

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].'
)

Unable to iterate random values in subsequent lines in Python

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()

Writing an ASCII file from a 2D numpy array

I have a 2D numpy array. I want to make a ASCII file out of it. Here's the code I am using where a3 is the numpy array
f = open('ASCIIout.asc', 'w')
numpy.savetxt(ASCIIout, a3)
f.write("ncols " + str(ncols) + "\n")
f.write("nrows " + str(nrows) + "\n")
f.write("xllcorner " + str(xllcorner) + "\n")
f.write("yllcorner " + str(yllcorner) + "\n")
f.write("cellsize " + str(cellsize) + "\n")
f.write("NODATA_value " + str(noDATA) + "\n")
f.close()
Now I open it with append option and write the 2D array values:
f_handle = open(ASCIIout, 'a')
numpy.savetxt(f_handle, MyArray, fmt="%.3f")
f_handle.close()
However, I have a couple problems. First, the fmt really does not work I get values like this:
9.999000000000000000e+03
If I JUST use the code below line of code, I get -9999.000 1.345, but then, I haven't attached ncols, nrows, etc to the ASCII file.
numpy.savetxt(f_handle, MyArray, fmt="%.3f")
My data range from 0 to 6. What I really want to get is:
-9999 1.345 -9999 3.21 0.13 -9999
where I do NOT get decimals after -9999 and I do get decimals after real data such as 1.345 I made the ASCII file in R, I am wondering there should be a easy way to do it in Python. Thanks for your help in advance.
If your intention is to have the "stop value" -9999 appear without the decimal point, you can let savetxt() save all the values with the "%.3f" format, and replace the one result you don't want with the one you want:
from StringIO import StringIO
import numpy as np
f = StringIO()
x = np.array(( -9999, 1.345, -9999, 3.21, 0.13, -9999), dtype=float)
np.savetxt(f, x, fmt='%.3f')
f.seek(0)
fs = f.read().replace('-9999.000', '-9999', -1)
f.close()
f = open('ASCIIout.asc', 'w')
f.write("ncols " + str(ncols) + "\n")
f.write("nrows " + str(nrows) + "\n")
f.write("xllcorner " + str(xllcorner) + "\n")
f.write("yllcorner " + str(yllcorner) + "\n")
f.write("cellsize " + str(cellsize) + "\n")
f.write("NODATA_value " + str(noDATA) + "\n")
f.write(fs)
f.close()
If you want all the integer values represented without decimals, .replace('.000', '', -1) would accomplish that.

Python won't convert tuple to string

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.

Categories