Saving variables in Python as columns without brackets - python

I have a code which returns some variables that I would like to later on use in another program. However, the output doesn't look like I want it to. The rows are within brackets [], and I would like to have them removed.
I have found the following question that deals with something similar; however, I am saving the variable, not printing it on the screen:
how to remove characters from printed output in python
This is where the variable is defined, the libraries used are chaospy and numpy.
nodes, weights = cp.generate_quadrature(order, dist, rule="G", sparse=True)
nodes_trans = nodes.transpose()
And this is where the variable is saved
with open('nodes_smolyak_trans.txt', 'w') as ndsT:
for itemndsT in nodes_trans:
ndsT.write("%s\n" % itemndsT)
ndsT.close()
Also, dist, mentioned above is defined as
dist = cp.J(wX, wY, pX, pY)
And all it's compoents are defined equally as
windX = cp.Uniform(0, 100)
Now, all rows of my output looks like this:
[50. 50. 50. 21.13248654]
I would like them to be instead simply
21.13248654
If the number of rows is small, I can manually remove them by hand, but when the output contains hundreds, I waste too much time, so any sugestion to remove the brackets manually is welcome.

My suspicion is that itemndsT is a list type, which is why when you write it to the file it includes the square brackets. You'll need to format it into a string yourself before writing it. There are a few ways you can do this, but using the join string function is one of the simplest:
for itemndsT in nodes_trans:
item_str = " ".join(map(str,itemndsT))
ndsT.write("%s\n" % item_str)

Related

Splitting a line into a list (with some difficulty)

I've been trying to work on an assignment and after hours of searching asking class mates and looking on here I can't work this out.
I have a text file containing +100000 lines of data in the format:
"Words" \t "float" \t "float"
and I'm trying to write a function which lets me search a line and pull out one piece of information. It works fine when I write it normally, but I cannot seem to put it into a function
FileList = Mammal.readlines()
Name,Latitude,Longitude = FileList[int].split("\t")
print (Name)
def LineToList(int):
FileList = Text.readlines()
A,B,C = FileList[int].split("\t")
LineToList(0)
print (A)
I receive this error.
IndexError: list index out of range
I've tried swapping out int for a letter, a ratio and adding lines to return values for A, B and C then printing them out but each time it fails.
Your code reads the entire contents of an open file with readlines(), then processes only one line. If your function looks exactly as you show, it will succeed the first time you use it (as long as it's used on a freshly opened file); but on the second call there will be nothing more for readlines() to read, and you'll get back an empty list.
Here's a simpler way to convert an entire file:
lines = mammal.readlines()
values = [ row.split("\t") for row in lines ]
You then have all your values in one list of triples.
Also, note the capitalization. Python style uses names that start with a capital for user-defined types, not for ordinary variables.

How can I make a list that contains various types of elements in Python?

I have the following parameters in a Python file that is used to send commands pertaining to boundary conditions to Abaqus:
u1=0.0,
u2=0.0,
u3=0.0,
ur1=UNSET,
ur2=0.0,
ur3=UNSET
I would like to place these values inside a list and print that list to a .txt file. I figured I should convert all contents to strings:
List = [str(u1), str(u2), str(u3), str(ur1), str(ur2), str(ur3)]
This works only as long as the list does not contain "UNSET", which is a command used by Abaqus and is neither an int or str. Any ideas how to deal with that? Many thanks!
UNSET is an Abaqus/cae defined symbolic constant. It has a member name that returns the string representation, so you might do something like this:
def tostring(v):
try:
return(v.name)
except:
return(str(v))
then do for example
bc= [0.,1,UNSET]
print "u1=%s u2=%s u3=%s\n"%tuple([tostring(b) for b in bc])
u1=0. u2=1 u3=UNSET
EDIT simpler than that. After doing things the hard way I realize the symbolic constant is handled properly by the string conversion so you can just do this:
print "u1=%s u2=%s u3=%s\n"%tuple(['%s'%b for b in bc])

Python : Assigning image to a variable using loop statement

I need to assign a image to three variables i.e dsp1, dsp2, dsp3 by looping. On execution, I get a Syntax error.
SyntaxError: can't assign to operator.
for i in range(0,3):
dsp+str(i)=Image.open("L1.jpg")
What is the problem with 'str(i)' ?
Can any one explain with simple example ?
Instead of generating dynamic variables, place these images in a list:
images = []
for i in range(3):
images[i] = Image.open("L1.jpg")
Using this method, the L1.jpg is assigned to the following:
images[0]
images[1]
images[2]
Alternatively, you can use a dictionary to get closer to the variable name format you are using:
images = {}
for i in range(3):
images['dsp' + str(i)] = Image.open("L1.jpg")
This produces a dictionary that has the following layout:
{
'dsp2': <image object>,
'dsp1': <image object>,
'dsp0': <image object>
}
You can access any of these images by using the key (ie. image['dsp1'])
In both of these cases, you don't need to worry about dynamic variables. Instead, everything you will be using sits in either a single list or dictionary.
You can not assign to an operator.
Look at your code line:
dsp + str(i) = Image.open("L1.jpg")
You have dsp + str(i) on the left side, an expression containing the sum operator +. Even if that would get evaluated properly, the result would be a string like "dsp1" for example. You can't assign any value to a string.
And because such uses make no sense, Python does not support operators on the left side of an assignment.
You want to dynamically create a variable name instead of hard-coding it. Although this is possible using exec(), that is strongly discouraged, as it easily leads to bugs in your code, is hard to read and even harder to debug. It may even be a security risk (code injection) if anything getting evaluated this way is untrusted data like user input.
What you should use instead is a simple list:
dsp = []
for i in range(0,3):
dsp[i] = Image.open("L1.jpg") # for whatever reason you open the same file 3 times...
You create a list simply using the square brackets. If you want to initialize it with some values, simply write them inside, separated by commas:
my_list = ["zero", 1, 2, "three", 4.5, True]
You access elements of a list by specifying an index value, starting with 0 for the first element:
print(my_list[3]) # output: three
You can also easily loop over all elements of a list:
for item in my_list:
print(item)
# output:
# zero
# 1
# 2
# three
# 4.5
# True
You can't just string text together to create a variable name like that unfortunately. The problem is with dsp+str(i)=, not just str(i).
If you absolutely must do it this way, you can do it using globals like so:
for i in range(0,3):
globals()["dsp" + str(i)] = Image.open("L1.jpg")
print(dsp1) # This will contain something
This will allow you to access those variables as if you had created them the 'normal' way first.
Ideally though, you should probably assign your results to a list instead, rather than discrete variable names.
The problem is that you're trying to generate variable names on the fly.
If I were you I would try to use a dictionary instead of generation dynamic variable names. If you REALLY need dynamic variable names I would go for something like this:
exec("dsp%d = Image.open(\"L1.jpg\")" % (i));
but I really do not recommend it. Just use a dictonary!

Theano shared updating last element in python

I have a shared variable persistent_vis_chain which is being updated by a theano function where it gets its function from a theano.scan, But thats not the problem just back story.
My shared variable looks like D = [image1, ... , imageN] where each images is [x1,x2,...,x784].
What I want to do is take the average of all the images and put them into the last imageN. That is I want to sum all the values in each image except the last 1, which will result in [s1,s2,...,s784] then I want to set imageN = [s1/len(D),s2/len(D),...s784/len(D)]
So my problem is I do not know how to do this with theano.shared and may be with my understanding of theano functions and doing this computation with symbolic variables. Any help would be greatly appreciated.
If you have N images, each of shape 28x28=784 then, presumably, your shared variable has shape (N,28,28) or (N,784)? This method should work with either shape.
Given D is your shared variable containing your image data. If you want to get the average image then D.mean(keepdims=True) will give it to you symbolically.
It's unclear if you want to change the final image to equal the mean image (sounds like a strange thing to do), or if you want to add a further N+1'th image to the shared variable. For the former you could do something like this:
D = theano.shared(load_D_data())
D_update_expression = do_something_with_scan_to_get_D_update_expression(D)
updates = [(D, T.concatenate(D_update_expression[:-1],
D_update_expression.mean(keepdims=True)))]
f = theano.function(..., updates=updates)
If you want to do the latter (add an additional image), change the updates line as follows:
updates = [(D, T.concatenate(D_update_expression,
D_update_expression.mean(keepdims=True)))]
Note that this code is intended as a guide. It may not work as it stands (e.g. you may need to mess with the axis= parameter in the T.concatenate command).
The point is that you need to construct a symbolic expression explaining what the new value for D looks like. You want it to be a combination of the updates from scan plus this additional average thing. T.concatenate allows you to combine those two parts together.

appending value in python and writing to a file

I have made a classifier in Python and it works fine. Now I need to output the results into a text file, which I can also do without a problem. The problem is, I need to include the id of the result as well as the result itself. I am new to python and still getting used to the syntax so any help would be greatly appreciated.
printedRow = '{id1},{result1}'.format(id1=id , result1 = result)
print("~~~~~RESULT~~~~~")
for k in range(0,len(pans)):
pans.append(row[0] + ','+ p.classify(row))
print (pans)
print(pans, file=outfile)
The id that I need to include with the results of the classifier is being held in the index row[0]. When I run this code, the same id is printed with every result. The results are printing out fine, I just need to be able to match the id with the result.
They are both held in lists and there are about 2000 values in each list.
I am not sure what you are trying to do because you don't define what row or pans are. Consider using more Pythonic source code. If an object is a collection of any type, then use some sort of iterator. Enumerate is useful to give each loop an index number.
for n, p in enumerate(pans):
print "pan %d is %s" % (n, p)
You also appear to be appending to pans within a loop whose size depends on pans. That could go on forever!
If you want to add to a list, just add it, e.g.:
newpans = pans + [x.classify(row) for x in oldpans]
Rather than building up a structure in memory, then printing it to a file, write directly to a file. This will use up far less memory. If you want speed, let the computer buffer the file for you, e.g.
f = open("output.txt", "w")
for n, p in enumerate(pans):
f.write("%s,%s\n" % (n, classify(p)))
f.close()

Categories

Resources