0

I have a text file with json data stored in UTF-8 like this:

{'name': u'احسان', 'family': u'شیرزادی'}

I've tried to read and print file data with this code:

file = open("output.txt", "r")
text = file.read()
file.close()
print text

It's OK and exactly as I can see in the file. but when I try to print some part of dictionary by indexes like this:

file = open("output.txt", "r")
text = file.read()
file.close()
print text['name']

An error says that:

    print text['name']
TypeError: string indices must be integers, not str

But when I run this code directly I can see It's working:

temp = {'name': u'احسان', 'family': u'شیرزادی'}
print temp['name']

What's the problem here?

ehsan shirzadi
  • 4,709
  • 16
  • 69
  • 112

4 Answers4

5

The result of file.read() is a string. Python cannot know that you want JSON and magically convert it.

There's a module inside of the standard library that can transform strings containing JSON into Python objects:

import json

with open('output.txt', 'r') as fobj:
    data = json.load(fobj)

print data['name']

Also, you should make sure that your JSON data is formatted correctly. As others have mentioned before me, JSON strings need double quotes. Single quotes give a syntax error. And you can't have characters like u outside of quotes.

data = {'name': u'احسان', 'family': u'شیرزادی'}

with open('output2.txt', w) as fobj:
    json.dump(data, fobj)

In the file output2.txt you will have correctly formatted JSON. To retrieve the data back into Python, you can do the same thing as above with the correct filename.

rubik
  • 8,814
  • 9
  • 58
  • 88
1

You have several problems. If the data really is stored like that, its not valid json and you will have to read it in as a string. And then you cant access it like a dictionary.

But if your data looks like this in the file (note there is no u in front of words, and double quotes are used):

{"name": "احسان", "family": "شیرزادی"}

then you can read it in as json and use it like a dictionary:

import json

with open("testing.txt") as file:
    data = json.loads(file.read())
    print data["name"]

Output will be:

احسان

Christer Nissen
  • 487
  • 2
  • 6
1

Data should be in JSON format and output of file.read() is string not python dictionary, your have to converts it by json.loads. And I recommend json.dumps for storage your JSON (text) file.

import json
data  = {'name': u'احسان', 'family': u'شیرزادی'}

file = open("output.txt", "w")
file.write(json.dumps(data))
file.close()
print data

file = open("output.txt", "r")
text = json.loads(file.read())
file.close()
print text['name']

Data after dump in JSON should be have double quotes; like this;

{"name": "\u0627\u062d\u0633\u0627\u0646", "family": "\u0634\u06cc\u0631\u0632\u0627\u062f\u06cc"} # coding: utf-8

or

{"name": "احسان", "family": "شیرزادی"}

more information JSON python : https://docs.python.org/2/library/json.html


Regards,

Bandhit Suksiri
  • 3,390
  • 1
  • 18
  • 20
0

Json string needs double quotes. Please check; http://json.org/example

Also you can take a look at this example which imports json library. Reading JSON from a file?

Community
  • 1
  • 1
AykutE
  • 344
  • 3
  • 15