Hi Stackoverflow community, I am just new in Python I hope you can help me.
I've tried many different programs, but I didn't get any results. Here is one:
import requests
url ="https://bboxxltd.atlassian.net/rest/servicedeskapi/servicedesk/CMS/queue/213/issue"
auth='XXXXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXX'
r = requests.get(url, auth=(auth))
data = r.json().get('summary')
print(data)
Output: None
I wanted to have in the "summary", in this example:
For example:
Output:
summary:REQUEST FOR DATA
When you do for in in data, the i variable will take the value of the keys of data, one at the time. So normally you would do data[i] inside the for i in data.
If id is a high level attribute of data, you can simply do data['id'] outside the for loop. Anyways, this all depends on the structure of the returned JSON.
From the screenshot, you are getting:
KeyError: 'summary'
Which means that data is not an Array but an Object. You need to go down the Object further in order to reach the Array you are looking for. You need to inspect the data object; one good way to do this is to call print(data.keys()), this way, you'll find the attributes that you can access from data, until you get the array you are after.
Once you know the structure of the response
# It looks like they array is multiple levels
# inside data, so it may look like this:
issues = data[key1][key2]...[keyn]
for issue in issues:
if issue['id'] == issue_id:
...
As Pynchia points out,
if you can reach the elements of the array correctly,
i.e. i is correct,
then access summary via the fields key:
print(i['fields']['summary'])
Also, please post text rather than images.
Images can't be searched and therefore aren't useful to future readers.
You're asking us to volunteer our time for free to solve your problem, and you should make it as easy as possible for us to do so.
Why not upload images of code on SO when asking a question?
EDIT
Your question is unclear.
It is straightforward to ask for all the elements it contains:
for k, v in i['fields']:
print(f'The value of {k} is {v}.')
In your example, one of those k keys will be 'summary'.
Related
I'm studying how to work with data right now and so I'm following along with a tutorial for working with Time Series data. Among the first things he does is read_csv on the file path and then use squeeze=True to read it as a Series. Unfortunately (and as you probably know), squeeze has been depricated from read_csv.
I've been reading documentation to figure out how to read a csv as a series, and everything I try fails. The documentation itself says to use pd.read_csv('filename').squeeze('columns') , but, when I check the type afterward, it is always still a Dataframe.
I've looked up various other methods online, but none of them seem to work. I'm doing this on a Jupyter Notebook using Python3 (which the tutorial uses as well).
If anyone has any insights into why I cannot change the type in this way, I would appreciate it. I'm not sure if I've misunderstood the tutorial altogether or if I'm not understanding the documentation.
I do literally type .squeeze("columns") when I write this out because when I write a column name or index, it fails completely. Am I doing that correctly? Is this the correct method or am I missing a better method?
Thanks for the help!
shampoo = pd.read_csv('shampoo_with_exog.csv',index_col= [0], parse_dates=True).squeeze("columns")
I would start with this...
#Change the the stuff between the '' to the entire file path of where your csv is located.
df = pd.read_csv(r'c:\user\documents\shampoo_with_exog.csv')
To start this will name your dataframe as df which is kind of the unspoken industry standard the same as pd for pandas.
Additionally, this will allow you to use a "raw" (the r) string which makes it easier to insert directories into your python code.
Once you are are able to successfully run this you can simply put df in a separate cell in jupyter. This will show you what your data looks like from your CSV. Once you have done all that you can start manipulating your data. While you can use the fancy stuff in pd.read_csv() I mostly just try to get the data and manipulate it from the code itself. Obviously there are reasons not to only do a pd.read_csv but as you progress you can start adding things here and there. I almost never use squeeze although I'm sure there will be those here to comment stating how "essential" it is for whatever the specific case might be.
Long time listener first time caller. I am new to Python, about 3 days into this and I cannot figure out for the life of me, what is happening in this particular instance.
I brought in an XLSX file as a dataframe called dfInvoice. I want to use groupby on two columns (indexes?) but something funky is happening I think. I can't see my new grouped dataframe with the code below.
uniqueLocation = dfInvoice.groupby(['Location ID','Location'])
When I call uniqueLocation, all that is returned is this:
<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x000001B9A1C61198>
I have two questions from here.
1) what the heck is going on? I followed these steps almost identically to this (https://www.geeksforgeeks.org/python-pandas-dataframe-groupby).
2) this string of text between the carets, what should I refer to this as? I didn't know how to search for this happening because I don't exactly understand what this return is.
I am trying to add the current dictionary to a Redis list using a dictionary comprehension and then to print out the first (aka current) keys and values of that list. I say current because this is a process I will be continuing with a while loop to have the list building over time, but I have to always access the first keys/values.
I am sure I am totally butchering this, but this is what I have:
adict = {"a":1,"b":2,"c":3}
{rserver.rpush("list",value) for value in adict}
print(float(rserver.lindex("list",0)))
I need to get a list of both keys and values back.
Help would be MUCH appreciated. Thanks!
I am not quite positive on what your redis-list should contain (please include your expected result in the question), but assuming it should at the end of inserts look something like this ["a:1", "b:1", "c:1"], you can achieve this with
adict = {"a":1,"b":2,"c":3}
for key,value in adict.items():
rserver.rpush("list", ":".join([key, value]))
print(float(rserver.lindex("list",0))) #>>> "a:1"
(as you have not included what interface rserver exactly is, it is a bit hard to guess on its exact behavior)
Objective
All I am trying to do is retrieve a single record from a specific table where the primary key matches. I have a feeling I'm greatly over complicating this as it seems to be a simple enough task. I have a theory that it may not know the variable value because it isn't actually pulling it from the Python code but instead trying to find a variable by the same name in the database.
EDIT: Is it possible that I need to wrap my where clause in an expression statement?
Attempted
My Python code is
def get_single_record(name_to_search):
my_engine = super_secret_inhouse_engine_constructor("sample_data.csv")
print("Searching for " + name_to_search)
statement = my_engine.tables["Users"].select().where(my_engine.tables["Users"].c.Name == name_to_search)
# Print out the raw SQL so we can see what exactly it's checking for
print("You are about to run: " + str(statement))
# Print out each result (should only be one)
print("Results:")
for item in my_engine.execute(statement):
print(item)
I tried hard coding a string in its place.
I tried using like instead of where.
All to the same end result.
Expected
I expect it to generate something along the lines of SELECT * FROM MyTable WHERE Name='Todd'.
Actual Result
Searching for Todd
STATEMENT: SELECT "Users"."Name", ...
FROM "Users"
WHERE "Users"."Name" = ?
That is an actual question mark appearing my statement, not simply my own confusion. This is then followed by it printing out a collection of all the records from the table, as though it successfully matched everything.
EDIT 2: Running either my own hard coded SQL string or the generated query by Alchemy returns every record from the table. I'm beginning to think the issue may be with the engine I've set up not accepting the query.
Why I'm Confused
According to the official documentation and third party sources, I should be able to compare to hardcoded strings and then, by proxy, be able to compare to a variable.
Many lines and points possible that I would like to be able to track when I line/point has been moused over. Is there any short codeable way of doing it or do I half to come up with hundreds/thousands of different element names.
I've tried
self.z[0].canvas.create_line()
self.z[1].canvas.create_line()
as well as
self.z(0).canvas.create_line()
self.z(1).canvas.create_line()
to only get back an error saying something like z can't be an integer, aka you can't do that stupid:)
Is there anyway to set up a nice for loop and create the lines/points and then be able to test test them once they are created. I can test the points the way I want to be able to test them but I would just like an easier way of creating the lines/points.
Worst case scenario is there a way of setting up something like
self.z1.canvas
self.z2.canvas
self.z3.canvas
but have 1,2,3 each be able to be increased through a for loop? I'm not sure if I have ever seen something like what I'm suggesting be made mention of or not.
Every time you create an item on a canvas, it returns a unique id. You can store that id in a list.
self.lines = []
for x in range(1000):
item = self.canvas.create_line(...)
self.lines.append(item)
That being said, you don't need to keep any of these in an array to " track when I line/point has been moused over.". You can set up bindings for that.