I'm working on a small task with excel sheet and python and the problem that I'm facing is i have few lines of code to perform string manipulation on the data which i fetch from the sheet. Since i got plenty of sheets,sometimes only limited number of sheets are required and couple of time whole excel sheet to perform string manipulation i can't write the same code everywhere so i thought of performing the operation once and storing it like oldvalue : newvalue so that whenever i read oldvalue i don't have to do manipulation again just fetch the newvalue from there. Now i tried using dictionary which is the best way to do it but the problem with using it is my key and value can both be repetitive and i don't want to update my previous entry with it. As per my knowledge we can't achieve it using dictionary. So what I'm asking is whether we have some kind of different data type to store it? Or do we actually need one? Can you help me figure out a way to solve it without using any data type?
EDIT :
The point is I'm getting the data from excel sheet and performing string manipulation on it and sometimes the key and the value are getting repetitive and since i'm using dictionary, it's updating previous value which i don't want to.
This will check if your dictionary contains a value for a specified key. If not, you can manipulate your string and save it for that key. If it does, it will grab that value and use it as your manipulated string.
""" Stuff is done. New string to manipulated is found """
if key not in dict:
value = ... #manipulated string
dict[key] = value
else:
manipulated_string = dict[key] #did this before, have the value already
Related
I have a python data dictionary that I am using to do some date comparisions. However when I loop through it I can't figure out how to access the DailyPlannerRequest value. I have tried doing a request.DailyPlannerRequest but the error that comes back is 'datetime.date' object has no attribute 'DailyPlannerRequest'. I've read a bit about trying to convert this dictionary value but can't find anything helpful. I am trying to do a loop and query a filter value and compare it to this data dictionary value.
I'm trying to do something like....
if request.day == day and event.daily_planner_request_name == request.DailyPlannerRequest :
I've done this lots of times before but the complication here is that one is a queryset and the other is a dictionary value that I'm trying to get to compare.
I've played a bit with the approach documented here Accessing dict keys like an attribute?
But can't seem to figure this out.
#Waldemar Podsiadlo answered this one. The answer he provided helped me with my troubleshooting...for key, value in dict.items()
I have implemented a program in VBA for excel to generate automatic communications based on user inputs (selections of cells).
Such Macro written in VBA uses extensively the listObject function of VBA
i.e.
defining a table (list object)
Dim ClsSht As Worksheet
Set ClsSht = ThisWorkbook.Sheets("paragraph texts")
Dim ClsTbl As ListObject
Set ClsTbl = ClsSht.ListObjects(1)
accessing the table in the code in a very logical manner:
ClsTbl being now the table where I want to pick up data.
myvariable= ClsTbl.listcolumns("D1").databodyrange.item(34).value
Which means myvariable is the item (row) 34 of the data of the column D1 of the table clstbl
I decided to learn python to "translate" all that code into python and make a django based program accesable for anyone.
I am a beginner in Python and I am wondering what would be the equivalent in python to listobject of VBA. This decision will shape my whole program in python from the beginning, and I am hesitating a lot to decide what is the python equivalent to listobject in VBA.
The main idea here getting a way where I can access tables-data in a readable way,
i.e. give me the value of column "text" where column "chapter" is 3 and column paragraph is "2". The values are unique, meaning there is only one value in "text" column where that occurs.
Some observations:
I know everything can be done with lists in python, lists can contain lists that can contain lists..., but this is terrible for readability. mylist1[2][3] (assuming for instance that every row could be a list of values, and the whole table a list of lists of rows).
I don't considered an option to build any database. There are multiple relatively small tables (from 10 to 500 rows and from 3 to 15 columns) that are related but not in a database manner. That would force me to learn yet another language SQL or so, and I have more than enough with python and DJango.
The user modifies the structure of many tables (chapters coming together or getting splitted.
the data is 100% strings. The only integers are numbers to sort out text. I don't perform any mathematical operation with values but simple add together pieces of text and make replacements in texts.
the tables will be load into Python as CSV text files.
Please indicate me if there is something not enough clear in the question and I will complete it
Would it be necesary to operate with numpy? pandas?
i.e give me the value of cell
A DataFrame using pandas should provide everything you need, i.e. converstion to strings, manipulation, import and export. As a start, try
import pandas as pd
df = pd.read_csv('your_file.csv')
print(df)
print(df['text'])
The entries of the first row will be converted to labels of the DataFrame columns.
I don't have any code right now, but if possible, where would i start after inputting my csv file?
Maybe there's an easier way of doing this, but once i can assign each cell its own variable i'd like to use pyad to validate if the variable is disabled or enabled in python against active directory
Decided to turn my comment into an answer.
You need to "assign every row in that column a separate variable", hence I assume you want every cell in that column to be its own separate variable
I am not exactly sure how you use pyad, but logically if you need to access each row by name, I would recommend using a Python dictionary to accomplish this task.
Before beginning to read through your CSV file, create an empty dictionary; as you read through your column, create new entries in the dictionary with key the variable name you wanted, and value the value of the cell. You can then access the values regularly but instead of variable_name you need to use dict_name["variable_name"].
EDIT: Sorry for the confusion I'll explain what the program is for. It's to keep track of a users new weight record. This file will only update when they have exceeded their previous weight record with a timestamp. I want the user to be able to see a time line of their progress for each lift using the timestamp. This is why i was using lift['key']={data:dict} So that they can reference each lift type and query the date for example lift['snatch']['5/25'] this will tell them what they maxed that day. But i can't seem to be able to write this to a csv file properly. Thank you for you time! Happy friday!
I've been researching for days and can't seem to figure out how to add data to a specific Fieldname which is a the highest level key in my dict.
The data i want to add is a dict in it's own.
How I vision it to look like in the CSV file:
snatch <> squat <> jerk
10/25:150lbs <> 10/25:200lbs <> 10/25:0lbs
So this is how it would look like when they created the file. How am I able to update just one field.
Say the user only squatted that day and wants to Append data to that Field.
What I have so far:
import time
import csv
lifts={}
csv_columns = ['snatch','squat','jerk']
creation = time.strftime('%M:%S', time.localtime())
lifts['snatch']={creation:'150lbs'}
lifts['squat']={creation:'200lbs'}
lifts['jerk']={creation:'0lbs'}
try:
with open(csv_file, 'w') as csvfile:
writer = csvDictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in lifts:
writer.writerow(lifts)
except IOError as (errno, sterror):
print("Error")
return
->One of my issues is that when it writers to the csv file it writes it over three times. Not quite sure why. It's the format I want but it's there three times.
-> I also want to implement this next code and write to the specific column, when i do so it writes null or blanks in the other columns.
lifts['jerk'].update({time.strftime('%M:%S', time.localtime() : '160lbs'})
Then out putting
snatch <> squat <> jerk
10/25:150lbs <> 10/25:200lbs <> 10/25:0lbs 10/26:160lbs
Sorry I'm new to python and not quit sure how to use this editor i want that result to land under the {10/25:0lbs} Just like it would show in excel.
It's important to keep track of what's going on here: lifts is a dictionary with strings for keys ("snatch", "squat", "jerk") and whose values are also dictionaries. This second level of dictionaries has timestamp strings for keys and strings as values.
I suspect that when you want to update the lifts['jerk'] dictionary, you don't use the same key (timestamp) as the existing entry.
It doesn't seem like you need a dictionary for the second level; consider using a list instead, but if you must, you can access like so: lifts['jerk'][lifts['jerk'].keys()[0]] which is rather hamfisted - again, consider either using a different data type for the values of your lifts dictionary or use keys that are easier to reference than timestamps.
EDIT: You could do something like lifts['jerk'] = {'timestamp':creation,'weight':'165lbs'} which requires some restructuring of your data.
pytables doesn't natively support python dictionaries. The way I've approached it is to make a data structure of the form:
tables_dict = {
'key' : tables.StringCol(itemsize=40),
'value' : tables.Int32Col(),
}
(note that I ensure that the keys are <40 characters long) and then create a table using this structure:
file_handle.createTable('/', 'dictionary', tables_dict)
and then populate it with:
file_handle.dictionary.append(dictionary.items())
and retrieve data with:
dict(file_handle.dictionary.read())
This works ok, but reading the dictionary back in is extremely slow. I think the problem is that the read() function is causing the entire dictionary to be loaded into memory, which shouldn't really be necessary. Is there a better way to do this?
You can ask PyTables to search inside the table, and also create an index on the key column to speed that up.
To create an index:
table.cols.key.createIndex()
To query the values where key equals the variable search_key:
[row['value'] for row in table.where('key == search_key')]
http://pytables.github.com/usersguide/optimization.html#searchoptim