I am able to update the tKinter entry widgets boxes using textvariables... the issue is that it add brackets '{}' in my desired data...
def showRecord(self):
connection = sqlite3.connect("../employee.db")
connection.text_factory = sqlite3.OptimizedUnicode
cursor = connection.cursor ()
cursor.execute ( '''SELECT "Scheduled Shift" FROM employee_details WHERE Ecode = "5568328"''' )
items = cursor.fetchall ()
self.Employee1_FirstDay_ActualShift.set(items[0])
self.Employee1_SecondDay_ActualShift.set(items[1])
self.Employee1_ThirdDay_ActualShift.set(items[2])
self.Employee1_FourthDay_ActualShift.set(items[3])
self.Employee1_FifthDay_ActualShift.set(items[4])
self.Employee1_SixthDay_ActualShift.set(items[5])
self.Employee1_SeventhDay_ActualShift.set(items[6])
connection.commit ()
connection.close ()
Seeking help pls... Need to remove those brackets as shown in fig.
The reason it is doing that is because you are setting the value of a string variable to a list. Tkinter is a thin wrapper around a tcl/tk interpreter, and tcl uses curly braces to preserve the list structure when converting the list to a string when a list element has spaces or other special characters.
The solution is to make sure you pass a string to the set method. Otherewise the list will be passed to tcl/tk and it will use it's own list-to-string conversion.
In your case, since items is a list (rows) of lists (columns) and each row has a single column, you would do something like this to insert column zero of row zero into self.Employee1_FirstDay_ActualShift:
row_0 = items[0]
col_0 = row_0[0]
self.Employee1_FirstDay_ActualShift.set(col_0)
To condense that to one line, combined with all of the other rows it would look something like the following. I've added some extra whitespace to make it easier to compare each line. Also, this assumes that items has seven rows, and each row has at least one column.
self.Employee1_FirstDay_ActualShift.set( items[0][0])
self.Employee1_SecondDay_ActualShift.set( items[1][0])
self.Employee1_ThirdDay_ActualShift.set( items[2][0])
self.Employee1_FourthDay_ActualShift.set( items[3][0])
self.Employee1_FifthDay_ActualShift.set( items[4][0])
self.Employee1_SixthDay_ActualShift.set( items[5][0])
self.Employee1_SeventhDay_ActualShift.set(items[6][0])
Related
I have a list of fields an am trying to create an unpivot expression with stack() in pyspark.
stack() requires the params: number, field name, then field value.
stack(30, 'field1', field1...)
I have a list of lists like
[['field1','field1'],['field2','field2']...]
I then can make a single list
['field1','field1','field2','field2']
But i need to remove the single quotes from the second occurence, so it works as the "field value"
unpivot_Expr = "stack(30, 'field1',field1,'field2',field2...)"
So far i'm getting stack(30, 'field1','field1','field2','field2'...)
I'm not sure how, or which is the easiest place to remove the single quotes? Any help is much appreciated.
Edit:
Sorry should've given context, I need to insert this string into a pyspark select expression
unpivot_df = df.select("hashKey", expr(unpivot_Expr))
Currently I drop the list into the string and replace the [] like this
unpivot_Expr = "stack({0}, {1})".format(str(len(fieldList)), str(fieldList).replace("[","").replace("]",""))
How about building up the string unpivot_Expr piece by piece via:
all_fields = [
['field1','field1'],
['field2','field2']
]
unpivot_Expr = "stack(30"
for pair in all_fields:
unpivot_Expr += f", '{pair[0]}', {pair[1]}"
unpivot_Expr += ")"
print(unpivot_Expr)
I think that will give you the tring you seek:
stack(30, 'field1', field1, 'field2', field2)
I'm trying to create a variable with multiple numbers which is then parsed into a code to delete multiple rows via Smartsheet API.
This is how it works:
smartsheet_client.Sheets.delete_rows(
3637721312342212, #sheet_id
[567387105060740, 4908041578801028]) #row_id
I would like to parse multiple numbers by assigning it to a variable beforehand, but it doesn't work. I wonder if the brackets are the problem when parsing the variable.
row_ids = 567387105060740, 4908041578801028
print(row_ids)
(567387105060740, 4908041578801028)
smartsheet_client.Sheets.delete_rows(
3637721312342212, #sheet_id
[row_ids]) #row_id
If I get the variable to work, I wonder how to transform all the values of one column (example below) into the same format to create a variable with the respective numbers.
From:
0 4738432649193348
1 5625432649192019
2 6321432649125716
Name: row_id_column, dtype: Int64
To (I guess tuple):
row_id_column = 4738432649193348, 5625432649192019, 6321432649125716
The .delete_rows() function requires a list of row ids (integers).
row_ids = 567387105060740, 4908041578801028
The above will create a tuple with the row ids. Instead, you need:
row_ids = [567387105060740, 4908041578801028]
From here, your code should look like:
row_ids = [567387105060740, 4908041578801028]
smartsheet_client.Sheets.delete_rows(
3637721312342212, #sheet_id
row_ids) # list of row ids
Note the removed []. Your current method [row_id] will send a list of tuples [(567387105060740, 4908041578801028)] instead of the required list of integers.
title = 'Example####+||'
blacklisted_chars = ['#','|','#','+']
for i in blacklisted_chars:
convert = title.replace(i, '')
print(convert)
# Example####||
I want to remove all blacklisted characters in a list and replace them with '', however when the code is run only the final 'blacklisted_char' is replaced within the print statement
I am wondering how I would make it that all characters are replaced and only 'Example' is printed
Strings are immutable in python. You assign a new string with
convert = title.replace(i, '')
title remains unchanged after this statement. convert is an entirely new string that is missing i.
On the next iteration, you replace a different value of i, but still from the original title. So in the end it looks like you only ran
convert = title.replace('+', '')
You have two very similar options, depending on whether you want to keep the original title around or not.
If you do, make another reference to it, and keep updating that reference with the results, so that each successive iteration builds on the result of the previous removal:
convert = title
for i in blacklisted_chars:
convert = convert.replace(i, '')
print(convert)
If you don't care to retain the original title, use that name directly:
for i in blacklisted_chars:
title = title.replace(i, '')
print(title)
You can achieve a similar result without an explicit loop using re.sub:
convert = re.sub('[#|#+]', '', title)
Try this :
title = 'Example####+||'
blacklisted_chars = ['#','|','#','+']
for i in blacklisted_chars:
title = title.replace(i, '')
print(title)
Explanation: Since you were storing the result of title.replace in the convert variable, every iteration it was being overwritten. What you need is to apply replace to the result of the previous iteration, which can be the variable with the original string or another variable containing a copy of it if you want to keep the original value unchanged.
P.S.: strings are iterables so you can also achieve the same results with something like this:
blacklisted_chars = '#|#+'
I would like to output dict data in the form of a table in the console:
dtc={ "test_case1_short":{"test_step11":"pass","test_step12":"pass","test_step_13":{"status":"failed","details":"ca marche po"},
"test_case2_longest_name":{"test_step21":"ne","test_step22":"ne"},
"test_case3_medium_name":{"test_step31":"ne","test_step32":"ne"} }
note: for french speakers 'dtc' is a shortcut for dict_test_collection (!)
To build this table I would like to determine the size of key names in order to dimension my column headers.
I can get my key names max length doing this:
max = 0
for i in list(dtc.keys()):
if max < len(i):
max = len(i)
print(max)
but I find this not very straighforward ... is there a way to get this information from dict.keys() or another dict feature ?
Besides, I'd like to set separators like "+-----------------------------+" for section headers and "| |" for section bodies, to have a good looking table.
In section bodies, is there a straight and simple way to set the table and columns width (ie. '|' caracter ending up at column 50, whatever the text on the line, like padding the line with spaces until a certain column)
Thank you
Alexandre
is there a way to get this information from dict.keys() or another dict feature ?
This seems straightforward to me:
max_key_len = max(len(key) for key in dtc)
print(max_key_len)
This one seems less straightforward, but it is shorter:
max_key_len = max(map(len, dtc))
print(max_key_len)
I have a column family with a secondary index 'pointer'. How do I remove multiple rows that have the same 'pointer' value (e.g. abc)?
The only option I know is:
expr = create_index_expression('pointer', 'abc')
clause = create_index_clause([expr])
for key, user in cassandra_cf.get_indexed_slices(clause):
cassandra_cf.remove(key)
but I know this is very inefficient and can take long if I have thousands of rows with the same 'pointer' value. Are there any other options?
You can remove multiple rows at once:
expr = create_index_expression('pointer', 'abc')
clause = create_index_clause([expr])
with cassandra_cf.batch() as b:
for key, user in cassandra_cf.get_indexed_slices(clause):
b.remove(key)
This will group the removes into batches of 100 (by default). When the batch object is used as a context manager as it is here, it will automatically handle sending any remaining mutations once the with block is left.
You can read more about this in the pycassa.batch API docs.