Importing text files using a loop - python-2.7

I want to import text files using a loop instead of importing them one by one.
I have 9 text files labelled clump0.txt - clump9.txt and after importing them I have to assign a variable.
ymport.textClumps('/tmp/clump1.txt')
#c0 = getClumpInfo()
c0 = pack.SpherePack()
c0.fromSimulation()
O.resetThisScene()
ymport.textClumps('/tmp/clump1.txt')
#c1 = getClumpInfo()
c1 = pack.SpherePack()
c1.fromSimulation()
O.resetThisScene()
ymport.textClumps('/tmp/clump2.txt')
#c1 = getClumpInfo()
c2 = pack.SpherePack()
c2.fromSimulation()
O.resetThisScene()
ymport.textClumps('/tmp/clump3.txt')
#c1 = getClumpInfo()
c3 = pack.SpherePack()
c3.fromSimulation()
O.resetThisScene()
...
Later I am using c0-c9 in this:
test = sp.makeClumpCloud(minCorner, maxCorner, [c0,c1,c2,c3,c4,c5,c6,c7,c8,c9], periodic=True, num = -1)
I already tried:
# load clumps
arr = [c1, c2, c3, c4, c5, c6, c7, c8, c9]
for i in range (0, 9):
ymport.textClumps("/tmp/clump{}.txt".format(i))
arr[i] =pack.SpherePack()
arr[i].fromSimulation()
O.resetThisScene()

Ok I solved it doing
load clumps
arr = [0,0,0,0,0,0,0,0,0]
for i in range (0, 9):
ymport.textClumps(str(os.getcwd()) + "/tmp/clump{}.txt".format(i))
arr[i] = pack.SpherePack()
arr[i].fromSimulation()
O.resetThisScene()
I just had to assign a placeholder into the array :) Thanks

Related

Google Sheets QUERY with WHERE on multiple columns at the same time

Following this post Google Sheets QUERY with WHERE on multiple columns I build up my formula to select specific values from more columns but when I had a condition data start to be confused and not equal to what I selected. I started from this query which works perfectly on one column for each IF:
=QUERY(Concerti!A1:AL; "SELECT * WHERE 1=1" &IF(A2="TUTTI";"";" AND E = '"&A2&"' ") &IF(E2="TUTTI";"";" AND H = '"&E2&"' ") &IF(F2="TUTTI";"";" AND B = "&F2&" "); 1)
Then I wrote this query to select (and filter) the content in A4, E4 and F4 but it doesn't work correctly i.e. it keeps some data which doesn't exactly match with the values on A4, E4 and F4.
=QUERY(Concerti!A1:AL; "SELECT * WHERE 1=1" &IF(A2="TUTTI";"";" AND E = '"&A2&"' ") &IF(E2="TUTTI";"";" AND H = '"&E2&"' ") &IF(F2="TUTTI";"";" AND B = "&F2&" ") &IF(A4="TUTTI";"";" AND I = ('"&A$4&"') OR (K='"&A$4&"' OR M='"&A$4&"' OR O='"&A$4&"' OR Q='"&A$4&"' OR S='"&A$4&"') ") &IF(E4="TUTTI";"";" AND J = ('"&E$4&"') OR (L='"&E$4&"' OR N='"&E$4&"' OR P='"&E$4&"' OR R='"&E$4&"' OR T='"&E$4&"') ") &IF(F4="TUTTI";"";" AND Y = ('"&F$4&"') OR (AA='"&F$4&"' OR AC='"&F$4&"' OR AE='"&F$4&"' OR AG='"&F$4&"' OR AI='"&E$4&"') "); 1)
Where is my mistake? Thank you so much in advance!!! I post a screenshot of the project:
[![Filtering mask](https://i.stack.imgur.com/cRFLv.png)](https://i.stack.imgur.com/cRFLv.png)
try:
=QUERY(Concerti!A1:AL; "where 1=1"&
IF(A2="TUTTI";;" and E = '"&A2&"'")&
IF(E2="TUTTI";;" and H = '"&E2&"'")&
IF(F2="TUTTI";;" and B = "&F2&" ")&
IF(A4="TUTTI";;" and (I = '"&A$4&"'
or K = '"&A$4&"'
or M = '"&A$4&"'
or O = '"&A$4&"'
or Q = '"&A$4&"'
or S = '"&A$4&"')")&
IF(E4="TUTTI";;" and (J = '"&E$4&"'
or L = '"&E$4&"'
or N = '"&E$4&"'
or P = '"&E$4&"'
or R = '"&E$4&"'
or T = '"&E$4&"')")&
IF(F4="TUTTI";;" and (Y = '"&F$4&"'
or AA= '"&F$4&"'
or AC= '"&F$4&"'
or AE= '"&F$4&"'
or AG= '"&F$4&"'
or AI= '"&E$4&"')"); 1)

How to delete entire row in Tkinter grid layout

I am trying a test application with Tkinter. I have created a table using grid layout manager as shown below. There are two buttons - Add row and Delete row. Add row works as expected. How i can go about deleting selected rows. My idea was to give a Checkbutton for each row as shown below. And every selected row can be deleted. But i don't know how to do it exactly and if its possible.
Is there a better way to delete the rows in this case ? please suggest a solution.
CODE:
from Tkinter import *
import ttk
from ttk import *
i =2
def add_row():
global i
var = IntVar()
c = Checkbutton(root, variable = var)
c.grid(row = i, column = 0)
for j in range(1,5): #Columns
b = Entry(root)
b.grid(row=i, column=j)
i =i+1
root = Tk()
bt = ttk.Button(root , text = 'Add Row', command = add_row)
bt.grid(row =0, column=0)
dl = ttk.Button(root , text = 'Delete Row')
dl.grid(row =0, column=1)
v0 = StringVar()
e0 = Entry(root, textvariable = v0, state = 'readonly')
v0.set('Select')
e0.grid(row = 1, column = 0 )
v1 = StringVar()
e1 = Entry(root, textvariable = v1, state = 'readonly')
v1.set('Col1')
e1.grid(row = 1, column = 1 )
v2 = StringVar()
e2 = Entry(root, textvariable = v2, state = 'readonly')
v2.set('Col2')
e2.grid(row = 1, column = 2)
v3 = StringVar()
e3 = Entry(root, textvariable = v3, state = 'readonly')
v3.set('Col3')
e3.grid(row = 1, column = 3 )
v4 = StringVar()
e4 = Entry(root, textvariable = v4, state = 'readonly')
v4.set('Col4')
e4.grid(row = 1, column = 4 )
mainloop()
Note: I do not want to use tktable or treeview to create tables.
In order to delete widgets, you need to keep a reference to them when they are created.
In the code below, I have created a list rows which has a sub-list items for each row. Each row contains a reference to the checkbutton and all the entries.
When Delete Row is pressed, the function loops through the list and destroys all items in the row that has the checkbutton activated, then removes the entry from the list.
Notes:
I have made var an attribute of the checkbutton, so that we can access it to see if it has been checked - see this question.
You were creating your checkbutton five times for each row; I've taken it out of the loop.
I iterate through the list of rows backwards, so that when an item is pop-ed, it only changes the index for the rows already processed. This means that you can delete multiple rows at once.
from Tkinter import *
import ttk
from ttk import *
i=2
rows = []
def add_row():
global i
i=i+1
items = []
var = IntVar()
c = Checkbutton(root, variable = var)
c.val = var
items.append(c)
c.grid(row = i, column = 0)
for j in range(1,5): #Columns
b = Entry(root)
items.append(b)
b.grid(row=i, column=j)
rows.append(items)
def delete_row():
for rowno, row in reversed(list(enumerate(rows))):
if row[0].val.get() == 1:
for i in row:
i.destroy()
rows.pop(rowno)
root = Tk()
bt = ttk.Button(root , text = 'Add Row', command = add_row)
bt.grid(row =0, column=0)
dl = ttk.Button(root , text = 'Delete Row', command = delete_row)
dl.grid(row =0, column=1)
v0 = StringVar()
e0 = Entry(root, textvariable = v0, state = 'readonly')
v0.set('Select')
e0.grid(row = 1, column = 0 )
v1 = StringVar()
e1 = Entry(root, textvariable = v1, state = 'readonly')
v1.set('Col1')
e1.grid(row = 1, column = 1 )
v2 = StringVar()
e2 = Entry(root, textvariable = v2, state = 'readonly')
v2.set('Col2')
e2.grid(row = 1, column = 2)
v3 = StringVar()
e3 = Entry(root, textvariable = v3, state = 'readonly')
v3.set('Col3')
e3.grid(row = 1, column = 3 )
v4 = StringVar()
e4 = Entry(root, textvariable = v4, state = 'readonly')
v4.set('Col4')
e4.grid(row = 1, column = 4 )
mainloop()

printing sorted columns based on values in python

I have a CSV file with 11 columns. I am new to Python and trying to make a code that sorted column 2 alphabetically (string) and then prints the rows of the sorted list.
I only want the rows printed where column 1 == 1.
If column 1 == 0 then i want the script to ignore this row and continue on to the next line without printing it.
in_file = open('filename.csv', 'r')
dict = {}
#should print all rows where field [0] == 1
print('List of rows where column one is equal to zero in alphabetical order of column 2')
for line in in_file:
line = line.strip('\n')
fields = line.split(',')
c1 = fields[0]
c2 = fields[1]
c3 = fields[2]
c4 = fields[3]
c5 = fields[4]
c6 = fields[5]
c7 = fields[6]
c8 = fields[7]
c9 = fields[8]
c10 = fields[9]
c11 = fields[10]
if c2 not in dict:
dict[c2] = c1
if c3 not in dict:
dict[c3] = c1
if c4 not in dict:
dict[c4] = c1
if c5 not in dict:
dict[c5] = c1
if c6 not in dict:
dict[c6] = c1
if c7 not in dict:
dict[c7] = c1
if c8 not in dict:
dict[c8] = c1
if c9 not in dict:
dict[c9] = c1
if c10 not in dict:
dict[c10] = c1
if c11 not in dict:
dict[c11] = c1
name = dict.keys()
sorted_names = sorted(name)
for name in sorted_names:
c1 = dict[name]
rows = [row for row in in_file if row ['c1']!= 1]
for row in rows:
print(c3, c2, c4, c5)
in_file.close()
As you commented, the error pops up in the 'rows = [row for row in in_file if row ['c1']!= 1]' line. As far as i know, the error is really simple:
you input indices as strings, but they Always have to be integers, as the error says. for example: your index is ['c1'], but c1 is a variable. using [c1] would probably solve the problem.

chunk of data into fixed lengths chunks and then add a space and again add them all as a string

I have got hex values as a85b080040010000. I want it to be as a8 5b 08 00 40 01 00 00. I have done it by using below code. But I have to work with very large data. So I want computed time to be very low.
import binascii
import re
filename = 'calc.exe'
with open(filename, 'rb') as f:
content = f.readline()
text = binascii.hexlify(content)
text1 = binascii.unhexlify(text)
length1 = 32
length2 = 16
list = re.findall('.{%d}' % length1, text)
list1 = re.findall('.{%d}' % length2, text1)
d = []
for i in range (0, len(list), 1):
temp = ""
l = re.findall('.{%d}' % length2, list[i])
s = l[0]
t = iter(s)
temp += str(' '.join(a+b for a,b in zip(t, t)))
temp += " "
s = l[1]
t = iter(s)
temp += str(' '.join(a+b for a,b in zip(t, t)))
temp += " | " + list1[i]
print temp
You can simply do
x="a85b080040010000"
print re.sub(r"(.{2})",r"\1 ",x)
or
x="a85b080040010000"
print " ".join([i for i in re.split(r"(.{2})",x) if i])

List index out of range ? It should not be

def trav(r,c):
#print("(",r,",",c,")")
ch = a[r+1][c+1]
lu= a[r][c]
#print(lu)
u = a[r][c+1]
#print(u)
ru= a[r][c+2]
#print(ru)
l = a[r+1][c]
#print(l)
r = a[r+1][c+2]
#print(r)
ld= a[r+2][c]
#print(ld)
d = a[r+2][c+1]
#print(d)
rd= a[r+2][c+2]
#print(rd)
a1 = a2 = a3 = a4 = a5 = a6 = a7 = a8 = 0
if(ch == lu-1):
a1 = trav(r-1,c-1)
if(ch == u-1):
a2 = trav(r-1,c)
if(ch == ru-1):
a3 = trav(r-1,c+1)
if(ch == l-1):
a4 = trav(r,c-1)
if(ch == r-1):
a5 = trav(r,c+1)
if(ch == ld-1):
a6 = trav(r+1,c-1)
if(ch == d-1):
a7 = trav(r+1,c)
if(ch == rd-1):
a8 = trav(r+1,c+1)
return max(a1,a2,a3,a4,a5,a6,a7,a8) + 1
while(1):
row,col = [int(x) for x in input().split(" ")]
if(row == 0):
break
a = []
for index in range(10):
a.append([])
for jindex in range(10):
a[index].append('a')
b = []
for index in range(row):
str = input()
for jindex in range(col):
a[index+1][jindex + 1] = ord(str[jindex])
if(str[jindex] == 'A'):
b.append([index,jindex])
#print (a)
#print (b)
ans = max([trav(x[0],x[1]) for x in b])
print(ans)
The code is generating the following error
Traceback (most recent call last): File "C:/Users/DELL/Desktop/ABCPATH.py", line 80, in <module>
ans = max([trav(x[0],x[1]) for x in b]) File "C:/Users/DELL/Desktop/ABCPATH.py", line 80, in <listcomp>
ans = max([trav(x[0],x[1]) for x in b]) File "C:/Users/DELL/Desktop/ABCPATH.py", line 17, in trav
ld= a[r+2][c] IndexError: list index out of range
But according to me it should not, and as I am extremely new to python , I am not able to debug it. Please help
sample input taken:
4 3
ABC
CFG
BDH
ABC
It has to break
r = a[r+1][c+2]
you're reassigning the value of 'r' in that line. please also note that you shouldn't name your string "str" as it is the name of the python string module and you won't be able to use it.