I am getting the error message : Traceback (most recent call last):
File "", line 10, in
TypeError: string indices must be integers . please help me.
the python 2.7 script sample:
import csv
cursor = db.job_templates.find( {}, {'_id': 1, 'rangePercents.questionnaire': 1, 'rangePercents.termMap': 1})
with open('range_percent1.csv', 'wt') as outfile:
fields = ['_id', 'questionnaire', 'termMap']
write = csv.DictWriter(outfile, fieldnames=fields)
write.writeheader()
for x in cursor:
x_id = x['_id']
for y in x['rangePercents']:
z = {
'_id': x_id,
'rangePercents.questionnaire': y['questionnaire'],
'rangePercents.termMap': y['termMap']}
write.writerow(z)
the data sample is
"_id": ObjectID("51dc52fec0d988a9547b5201"),
"rangePercents": {
"termMap": 0,
"questionnaire": 100
}
Don't iterate over x['rangePercents'], you already can access everything in it through x. If you iterate like you're doing, you just get each key of your dictionary as y, which is a string and thus why you get your error. Try this:
for x in cursor:
x_id = x['_id']
y = x['rangePercents']
z = {
'_id': x_id,
'rangePercents.questionnaire': y['questionnaire'],
'rangePercents.termMap': y['termMap']}
write.writerow(z)
Related
I'm trying to delete elements from _notes that have _type as 1, but i keep getting an error and I'm not sure what it means, nor do I know how to fix it.. can anyone help me?
My trimmed JSON:
{
"_notes": [
{
"_time": 10,
"_lineIndex": 2,
"_lineLayer": 0,
"_type": 0,
"_cutDirection": 7
},
{
"_time": 12,
"_lineIndex": 2,
"_lineLayer": 0,
"_type": 1,
"_cutDirection": 1
},
{
"_time": 14,
"_lineIndex": 2,
"_lineLayer": 1,
"_type": 1,
"_cutDirection": 0
}
]
}
My python script:
#!/usr/bin/python3
import json
obj = json.load(open("ExpertPlusStandardd.dat"))
for i in range(len(obj["_notes"])):
print(obj["_notes"][i]["_type"])
if obj["_notes"][i]["_type"] == 1:
obj.pop(obj["_notes"][i])
open("test.dat", "w").write(
json.dumps(obj, indent=4, separators=(',', ': '))
)
Error:
Traceback (most recent call last): File "C:\programming\python\train_one_hand\run.py", line 9, in <module> obj.pop(obj["_notes"][i]) TypeError: unhashable type: 'dict'
It is usually a bad idea to delete from a list that you're iterating. Reverse iterating avoids some of the pitfalls, but it is much more difficult to follow code that does that, so usually you're better off using a list comprehension or filter.
obj["_notes"] = [x for x in obj["_notes"] if x["_type"] != 1]
This gives us the expected output :
{'_notes':
[
{
'_time': 10,
'_lineIndex': 2,
'_lineLayer': 0,
'_type': 0,
'_cutDirection': 7
}
]
}
So I'm, getting the error:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/db/dm testing.py", line 12, in <module>
for id in ids['id']:
TypeError: string indices must be integers
With the following code:
import requests
import json
token = 'mytoken'
headers = {
'Authorization': token,
}
r = requests.get('https://canary.discordapp.com/api/v6/users/#me/channels', headers=headers).json()
ids=json.dumps(r)
for id in ids['id']:
print(id)
The JSON looks like this:
[
{
"id": "771634716042461195",
},
{
"id": "771654126345781278",
},
{
"id": "771658044526034967",
}
]
I want to print out the channel id's, which in this case are:
771634716042461195 and 771654126345781278.
Any help greatly appreciated.
Ok, so I found the solution, here is my final code:
import requests
import json
token = 'mytoken'
headers = {
'Authorization': token,
}
r = requests.get('https://canary.discordapp.com/api/v6/users/#me/channels', headers=headers).json()
x = 0
for i in range(len(r)):
print(r[x]['id'])
x+=1
It prints all the ID's of the channels. Hopefully this can help some other people out.
You are converting your parsed json back into a string, but you want it to be a dictionary, not a string. You can completely remove the second line.
r = requests.get('https://canary.discordapp.com/api/v6/users/#me/channels', headers=headers).json()
for id in r['id']:
print(id)
Hello I would like to get all the 'Integer' values from a dict:
array_test = [{ "result1" : "date1", "type" : "Integer"},{ "result1" : "date2", "type" : "null"}]
I tried:
test = {'result1':array_test['result1'] for element in array_test if array_test['type'] == "Integer"}
However I got this error:
>>> test = {'result1':array_test['result1'] for element in array_test if array_test['type'] == "Integer"}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <dictcomp>
TypeError: list indices must be integers or slices, not str
>>>
>>>
So I would like to appreciate support to achieve the following output
test = [{ "result1" : "date1", "type" : "Integer"}]
You need a list-comprehension, not dictionary-comprehension:
array_test = [{ "result1" : "date1", "type" : "Integer"},{ "result1" : "date2", "type" : "null"}]
test = [x for x in array_test if x['type'] == 'Integer']
# [{'result1': 'date1', 'type': 'Integer'}]
Why? Because required output is a list (list of dictionaries).
So i want to be able to pull data based on a certain condition, from this data i then want to be able to print multiple items from this query...here's what ive done so far:
def rec():
qe = JsonQ(r"C:\ShopFloor\data.json")
res = qe.at('data').where('Status', '=', 1).get()
for Process, Shortnum in res:
print(Process['Process'] + " " + Shortnum['Shortnum'])
rec()
this is from the following json file:
{
"data": [
{
"Shortnum": "34567",
"Process": "SPA",
"Status": 1,
"Start_Time": "2016-12-14 15:54:35",
"Finish_Time": "2016-12-14 15:56:02"
},
{
"Shortnum": "34567",
"Process": "Figure",
"Status": 0,
"Start_Time": "2016-12-08 15:34:05",
"Finish_Time": "2016-12-08 15:34:09"
},
How can I get this to work properly? Ideally I am looking for this kind of response from the print:
SPA 34567
cannot get a current output, i get this error... i realise I am passing too many arguments however i couldn't think of a proper way to do it...
Exception has occurred: ValueError
too many values to unpack (expected 2)
File "C:\ShopFloor\main.py", line 101, in rec
for Process, Shortnum in res:
File "C:\ShopFloor\main.py", line 106, in <module>
rec()
The typical approach to working with JSON in Python is to load the JSON object as a Python dict:
import json
with open('C:/ShopFloor/data.json', 'r') as f:
qe = json.load(f)
for item in qe['data']:
if item['Status'] == 1:
print(f'{item["Process"]} {item["Shortnum"]}')
Note this uses Python 3's f-strings (be sure to alternate single and double quotes when accessing dictionary values in an f-string). In Python 2, replace last line with:
print('{} {}'.format(item['Process'], item['Shortnum']))
What i have to parse :
I have a tsv file that looks like this :
https://i.stack.imgur.com/yxsXD.png
What is the end goal:
My goal is to read the tsv file and populate the contents of the csv file in a dictionary and nested lists without using csv parser.
In the end the in_memory_table structure would look
like this ( of course with more than two rows ):
{
"header": [
"STATION",
"STATION_ID",
"ELEVATION",
"LAT",
"LONG",
"DATE",
"MNTH_MIN",
"MNTH_MAX"
],
"rows": [
[
"Tukwila",
"12345afbl",
"10",
"47.5463454",
"-122.34234234",
"2016-01-01",
"10",
"41"
],
[
"Tukwila",
"12345afbl",
"10",
"47.5463454",
"-122.34234234",
"2016-02-01",
"5",
"35"
],
]
}
My code looks like this:
in_memory_table = {
'header': [],
'rows': [] }
with open('fahrenheit_monthly_readings.tsv') as f:
in_file = f.readlines()
i = 0
for line in in_file:
temp_list = [line.split('\t')]
if (i == 0):
in_memory_table['header']= line
elif(i != 0):
in_memory_table['rows'].append(line)
i += 1
print("\n",in_memory_table)
Output of the code:
C:\Users\svats\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/svats/PycharmProjects/BrandNew/module4_lab2/module4_lab2.py
{'header': 'STATION\tSTATION_ID\tELEVATION\tLAT\tLONG\tDATE\tMNTH_MIN\tMNTH_MAX\n', 'rows': ['Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-01-01\t10\t41\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-02-01\t5\t35\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-03-01\t32\t47\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-04-01\t35\t49\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-05-01\t41\t60\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-06-01\t50\t72\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-07-01\t57\t70\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-08-01\t68\t79\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-09-01\t55\t71\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-10-01\t47\t77\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-11-01\t32\t66\n', 'Tukwila\t12345afbl\t10\t47.5463454\t-122.34234234\t2016-12-01\t27\t55\n']}
Help needed:
i am very close towards getting the solution
I have 2 questions :
1. how to get rid of the \t in the o/p?
2. My o/p is little different from the desired o/p. how do i get it ?
If you rewrite your code as:
for line in in_file:
print('repr(line) before :', repr(line) )
temp_list = [line.split()]
#line = line.split()
print('temp_list :',temp_list)
print('repr(line) after :', repr(line) )
print(' %s -----------------' % i)
if ........
and de-comment the line #line = line.split()
you'll understand the reason of the bad result you obtain.
The reason is that line.split() doesn't change the object of name line ,
it creates a new object (the list you want) to which name line must be re-assigned if you want this name to refer to the obtained list.
Note that the method str.split([sep[, maxsplit]]) has a different algorithm according if parameter sep is None or not None, see documentation https://docs.python.org/2/library/stdtypes.html#str.split for this point
.
That said, there's a better way.
with open('fahrenheit_monthly_readings.tsv','r') as f:
in_memory_table = {'header':next(f).split()}
in_memory_table['rows'] = [line.split() for line in f]
or
with open('fahrenheit_monthly_readings.tsv','r') as f:
in_memory_table = {'header':next(f).split()}
in_memory_table['rows'] = list(map(str.split, f))