How can I List unique characters in a dictionary and store them as a set? - dictionary

I am trying to list unique characters in a dictionary and store them as a set. The dictionary has the following fields
ID,Name, Description, Type Price.
I need to list the unique categories in "Type" field
content=("C:\\Users\\jon.welsh\\Desktop\\ebyayproducts.json", "r")
for item in ebayproducts:
values = set([i['Type'] for i in content])
# and then I get this Error
> TypeError: string indices must be integers

Based on your example, you don't open the file - you just create a tuple content that contains 2 string values.
To open a file, parse the json you can do:
import json
with open("C:\\Users\\jon.welsh\\Desktop\\ebyayproducts.json", "r") as f_in:
content = json.load(f_in)
values = set(i["Type"] for i in content)
print(values)

Related

How do I split this array of strings into table rows in U-SQL?

I was trying this snippet to split my json array.
activity =
//to extract json object required, only "activity" field is to be parsed and not "nameOfWebsite"
EXTRACT activities : string
FROM #input
USING Extract.Json(rowPath: "[]");
activities_arr =
SELECT
//splitting into array based on delimiter
new ARRAY<string>(activities.Split(',')) AS activities
FROM activity
;
activities_output =
SELECT activities
FROM activities_arr AS ac
CROSS APPLY EXPLODE(ac.activities) AS activities //to split above array into rows
;
Input is like this
[
{
"nameOfWebsite": "StackOverflow", // this object is not required
"activities": [
"Python",
"U-SQL",
"JavaScript"
]
}
]
So, currently I am getting output as: 5 columns with one column as some random string not in input followed by 3 blank columns and then the 5th column contains Python, U-SQL, JavaScript in separate rows.
Questions:
Is there any way to avoid the 4 other columns as I only require data 4th column ie. only the name of activities?
Why are there blank spaces in my current output when my delimiter is defined as ','?
Current output ("blank" denotes blank space and not string blank)
AB#### "blank" "blank" "blank" Python
AB#### "blank" "blank" "blank" U-SQL
AB#### "blank" "blank" "blank" JavaScript
Output expected
Python
U-SQL
JavaScript

Python requests.get does not take field names for data

I need to generate data using the below url via requests.get :
fields = ("")
response_2 = requests.get(BASEURL + 'services/v17.0/report-jobs/' + jobId + "?fields=" +fields ,
headers = header_param)
For the purpose of the question, both the BASEURL and the JobID are pre defined.
However, there are several field names in the dataset such as Date, [Agent Name], [Agent ID] etc. that I'm looking to generate.
When I leave the fields object blank, no data is generated.
When I try to define the fields object using
fields = ("Date, Agent Name")
or
fields = ("Date", "Agent Name")
I always get back the error : Invalid fields argument
What is the best way to fix this?
I'm not sure what result you want, but the problem is you're trying to concatenate a string and a tuple, and misusing a tuple.
requests.get(BASEURL + 'services/v17.0/report-jobs/' + jobId + "?fields=".join(str(i) for i in fields)

Splitting Columns in USQL

I am new to USQL and I am having a hard time splitting a column from the rest of my file. With my EXTRACTOR I declared 4 columns because my file is split into 4 pipes. However, I want to remove one of the columns I declared from the file. How do I do this?
The Json column of my file is what I want to split off and make you new object that does not include it. Basically splitting Date, Status, PriceNotification into the #result. This is what I have so far:
#input =
EXTRACT
Date string,
Condition string,
Price string,
Json string
FROM #in
USING Extractor.Cvs;
#result =
SELECT Json
FROM #input
OUTPUT #input
TO #out
USING Outputters.Cvs();
Maybe I have misunderstood your question, but you can simply list the columns you want in the SELECT statement, eg
#input =
EXTRACT
Date string,
Status string,
PriceNotification string,
Json string
FROM #in
USING Extractor.Text('|');
#result =
SELECT Date, Status, PriceNotification
FROM #input;
OUTPUT #result
TO #out
USING Outputters.Cvs();
NB I have switched the variable in your OUTPUT statement to be #result. If this does not answer your question, please post some sample data and expected results.

Error binding parameter 0 - probably unsupported type

I am creating an SQL db and trying to iterate over an excel file and put all the data in to the SQL table as follows but I keep getting an annoying error. I have looked at the data types and still can't get my head around it please let me know if anyone spots what the problem is my code is:
import sqlite3
from openpyxl import load_workbook
#wb = load_workbook(r"LeaguePlayers.xlsx")
#read workbook to get data
wb = load_workbook(filename = r"LeaguePlayers.xlsx", use_iterators = True)
ws = wb.get_sheet_by_name(name = 'Sheet1')
#ws = wb.worksheets
conn = sqlite3.connect("players.db") # or use :memory: to put it in RAM
cursor = conn.cursor()
# create a table
cursor.execute("""CREATE TABLE players
(player TEXT,
team TEXT,
points INTEGER,
cost REAL,
position TEXT)
""")
#Iterate through worksheet and print cell contents
for row in ws.iter_rows():
for cell in row:
cursor.execute("INSERT INTO players VALUES (?,?,?,?,?)", row)
conn.commit()
#----------------------------------------
# display SQL data
#----------------------------------------
c.execute('SELECT * FROM players')
for row in c:
print (row)
The error i get says:
cursor.execute("INSERT INTO players VALUES (?,?,?,?,?)", row)
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
I really think you need to do some kind of introduction to Python.
You are making two elementary mistakes: looping of the cells in a row but passing the row to the query; passing a complex object as opposed to a native Python type such as an integer or string.
Something like the following is what you want:
player = [cell.value for cell in row]
cursor.execute(query, player)
Note, that execute takes a sequence (tuple or list) as the second argument.

ASP.NET:Get data into Radiobuttonlist from database

I am trying to insert data into radiobuttonlist from the database. but i get this error: Index was out of range. Must be non-negative and less than the size of the collection.
even though i have set the datatype of the field as VARCHAR(MAX). and the data in that field is a simple 4 letter word..say 'john'.
my code for inserting data is like this.:
RadioButtonList1.Items[0].Text = obj.dr["op1"].ToString();
RadioButtonList1.Items[1].Text = obj.dr["op2"].ToString();
RadioButtonList1.Items[2].Text = obj.dr["op3"].ToString();
RadioButtonList1.Items[3].Text = obj.dr["op4"].ToString();
Here's an example of how to bind a list to a DataSet. The example calls a stored proceedure:
http://codersbarn.com/post/2008/10/12/Bind-CheckBoxList-to-DataSet.aspx

Resources