You should pass the file contents (i.e. a string) to json.loads(), not the file object itself. Try this:
with open(file_path) as f:
data = json.loads(f.read())
print(data[0]['text'])
There's also the json.load() function which accepts a file object and does the f.read() part for you under the hood.
Error reading JSON from text IO stream under Python 3
python - TypeError: Object of type TextIOWrapper is not JSON serializable I don't know how to handle this - Stack Overflow
can't read json file with python. getting type error: json object is 'TextIOWrapper' - Stack Overflow
python - Reading JSON object: "TypeError: '_io.TextIOWrapper' object is not subscriptable" - Stack Overflow
You should pass the file contents (i.e. a string) to json.loads(), not the file object itself. Try this:
with open(file_path) as f:
data = json.loads(f.read())
print(data[0]['text'])
There's also the json.load() function which accepts a file object and does the f.read() part for you under the hood.
Use json.load(), not json.loads(), if your input is a file-like object (such as a TextIOWrapper).
Given the following complete reproducer:
import json, tempfile
with tempfile.NamedTemporaryFile() as f:
f.write(b'{"text": "success"}'); f.flush()
with open(f.name,'r') as lst:
b = json.load(lst)
print(b['text'])
...the output is success.
json.load() is for loading a file. json.loads() works with strings.
3 ways to load a json file:
import json
import ast
with open(file_path) as file:
data1 = json.load(file)
data2 = json.loads(file.read())
data3 = ast.literal_eval(file.read())
You should use json.load whenever possible, but sometimes the JSON file is not strictly in the correct format (e.g. single quotes instead of double quotes). A solution is to use ast.literal_eval().
I'm supposed to create a dictionary from the values in a file and search for 'Polly' and remove it if it's in the file,
I kept getting errors so I'm just trying to get it to print the value just so I can see what I'm doing wrong and even at the most simple level I keep getting errors,
I'm in my first semester so this is all still new to me
employees = {}
employees = open('dictionary_values.txt', 'r')
print(employees['Polly'])
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(employees['Polly'])
TypeError: '_io.TextIOWrapper' object is not subscriptable
Hello!
I am grabbing a some data off of an API. I then am trying to take the variable in which I have stored the gathered data and write it to a text file.
When I define the type my variable is it shows
<class '_io.TextIOWrapper'>
I have tried to figure out how to convert it to a string (as that what the error yells at me for) but I cannot find the function.
import time, json, requests
def btstamp():
bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
return bitStampTick.json()['last']
bitstampprice = (btstamp())
print (bitstampprice)
bitstampprice = open("testwrite1.txt", "w+")
print (type(bitstampprice))
bitstampprice.write(bitstampprice)
bitstampprice.close()Here is the error
C:\TradingBot>export.py
6456.95
<class '_io.TextIOWrapper'>
Traceback (most recent call last):
File "C:\TradingBot\export.py", line 15, in <module>
bitstampprice.write(bitstampprice)
TypeError: write() argument must be str, not _io.TextIOWrapper
C:\TradingBot>I have spent a good amount of time doing this and its getting late. Any help would be greatly appreciated.
I go into some info about _io.TextIOWrapper and the info is a bit advance for me.
Why is my data being returned in this type?
How come it is not a integer as it is shown when I "print" the variable?
Thanks
Aluad