Jump to content

"string indices must be integers"

Pythons
Go to solution Solved by Mr_KoKa,

The "saving it" part is wrong. Just ditch this line

jsonData = json.dumps(data) # Here is '{"Attack": "1"}'

 

and change this line

json.dump(jsonData, f) # And now it is '"{\"Attack\": \"1\"}"'

to

json.dump(data, f) # And now it is '"{\"Attack\": \"1\"}"'

 

 

So now you don't encode data to json and save it to jsonData and then encode it again and save to file, but now you just encode your data (dictionary) once right into the file. And then you load it from file as you did, decoding it once. Once encoded once decoded, it just works.

Hey,
So I've created a save file using the code:

Attack = 1

data = {

             "Attack:  str(Attack)

            }

jsonData = json.dumps(data)
with open("Savefile.json", 'w') as f:
             json.dump(jsonData, f)

 

But when i try to assign it to a variable using

 

with open('Savefile.json', 'r') as f:
            data = json.load(f)

Attack = data["Attack"]

 

It doesn't assign the 'Attack' to the variable Attack. It instead tells me that "TypeError: string indices must be integers"

Please help!! :(

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Pythons said:

Hey,
So I've created a save file using the code:

Attack = 1

data = {

             "Attack:  str(Attack)

            }

jsonData = json.dumps(data)
with open("Savefile.json", 'w') as f:
             json.dump(jsonData, f)

 

But when i try to assign it to a variable using

 

with open('Savefile.json', 'r') as f:
            data = json.load(f)

Attack = data["Attack"]

 

It doesn't assign the 'Attack' to the variable Attack. It instead tells me that "TypeError: string indices must be integers"

Please help!! :(

Just to be sure, and for other people to help, what language is this?

 

(I'm pretty sure it's python, but I can't be too sure.)

Want to know which mobo to get?

Spoiler

Choose whatever you need. Any more, you're wasting your money. Any less, and you don't get the features you need.

 

Only you know what you need to do with your computer, so nobody's really qualified to answer this question except for you.

 

chEcK iNsidE sPoilEr fOr a tREat!

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, bob51zhang said:

Just to be sure, and for other people to help, what language is this?

 

(I'm pretty sure it's python, but I can't be too sure.)

Sorry, I forgot to say that!
Yes, it is python

Link to comment
Share on other sites

Link to post
Share on other sites

can we see the whole script please?

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

oh, and you need to close the " quotes on the line that goes:

"Attack: str(Attack)

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

You encode your dictionary to json, and then you encode that json string to json again resulting in saving single string encoded in json to your file, when reading, you decode it once, so you ending up with a json string, not a dictionary.

 

Btw, python relies on indentation, and your code embedded in your post without code tags looks like garbage.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I had to cut out irrelevant code, the script is pretty long.
attack = 1
data = {
            "Attack":       str(attack),
        }

jsonData = json.dumps(data)

        with open("Savefile.json", 'w') as f:
             json.dump(jsonData, f)
        for i in str(.1):
            time.sleep(int(1))

 

This is to create a file called "Savefile.json"

 

with open('Savefile.json', 'r') as f:
            data = json.load(f)

attack = data['Attack']

for i in str(.1):
       time.sleep(int(1))

 

This is meant to load from a file called "Savefile.json"

 

  File "E:\PYTHON\Savefilecode\Save.py", line 6021, in load
    attack = data["Attack"]
TypeError: string indices must be integers

 

This is the error that i get

 

 

1 minute ago, Mr_KoKa said:

You encode your dictionary to json, and then you encode that json string to json again resulting in saving single string encoded in json to your file, when reading, you decode it once, so you ending up with a json string, not a dictionary.

 

Btw, python relies on indentation, and your code embedded in your post without code tags looks like garbage.

 

Look, I never said that i was any good at this, nor as good as anyone else in this forum. That's why I'm asking for help -_-
I actually hardly know much about this at all, I just need someone to point out the flaws and help me fix them!

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not judge your code, but the lack of use of code tags, look how much better it looks with code tags:

 

import json

Attack = 1
data = {
  "Attack":  str(Attack)
}

jsonData = json.dumps(data) # Here is '{"Attack": "1"}'
with open("savefile.json", 'w') as f:
  json.dump(jsonData, f) # And now it is '"{\"Attack\": \"1\"}"'

with open('savefile.json', 'r') as f:
  data = json.load(f) # You decode it once and it becomes '{"Attack": "1"}' (a string)
  Attack = data["Attack"] # You're trying to get "Attack" indice of string which can be only indiced by integers from 0 to len(data)-1

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Mr_KoKa said:

I'm not judge your code, but the lack of use of code tags, look how much better it looks with code tags:

 


import json

Attack = 1
data = {
  "Attack":  str(Attack)
}

jsonData = json.dumps(data) # Here is '{"Attack": "1"}'
with open("savefile.json", 'w') as f:
  json.dump(jsonData, f) # And now it is '"{\"Attack\": \"1\"}"'

with open('savefile.json', 'r') as f:
  data = json.load(f) # You decode it once and it becomes '{"Attack": "1"}' (a string)
  Attack = data["Attack"] # You're trying to get "Attack" indice of string which can be only indiced by integers from 0 to len(data)-1

 

Oh okay, I see your point now ^_^
How would I load the Integer from the .json file and then assign it back to a variable?

Link to comment
Share on other sites

Link to post
Share on other sites

The "saving it" part is wrong. Just ditch this line

jsonData = json.dumps(data) # Here is '{"Attack": "1"}'

 

and change this line

json.dump(jsonData, f) # And now it is '"{\"Attack\": \"1\"}"'

to

json.dump(data, f) # And now it is '"{\"Attack\": \"1\"}"'

 

 

So now you don't encode data to json and save it to jsonData and then encode it again and save to file, but now you just encode your data (dictionary) once right into the file. And then you load it from file as you did, decoding it once. Once encoded once decoded, it just works.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×