TypeError: string indices must be integers datetime

user6754289

I'm trying to convert a datetime, but I can't figure out how to get my string to be an integer. I've tried adding in [0] after (entry["created_at"] but not luck

Code:

$for item in data:

   `$` convertedTweet = {}

for entry in item:
   convertedTweet["created_at"] = item["created_at"]
   fmt = '%Y-%m-%dT%H:%M:%SZ'
   temp = datetime.strptime(entry['created_at'],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
   print temp.strftime(fmt)

Error:

temp = datetime.strptime(entry['created_at'],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
TypeError: string indices must be integers
Anand S Kumar

Looks like item is a dictionary ( as you do this - item["created_at"] , and it does not error out for you ) .

If so, when you loop through the dictionary as -

for entry in item:

entry represents the keys in the dictionary, which most probably are strings. So when you try to do - entry['created_at'] - it errors out as you are trying to use string indices for strings , which is not possible.

It does not look like you need loop at all, you can directly access - item["created_at"] - to get the datetime. Example -

convertedTweet["created_at"] = item["created_at"] 
temp = datetime.strptime(item["created_at"],'%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)
print temp.strftime('%Y-%m-%dT%H:%M:%SZ')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeError at / string indices must be integers

From Dev

Python ---- TypeError: string indices must be integers

From Dev

zeromq: TypeError: string indices must be integers, not str

From Dev

Scrapy: TypeError: string indices must be integers, not str?

From Dev

TypeError: string indices must be integers (Python)

From Dev

Reading a JSON string | TypeError: string indices must be integers

From Dev

TypeError list indices must be integers

From Dev

TypeError: string indices must be integers, not str // working with dict

From Dev

Python and JSON Error - TypeError: string indices must be integers

From Dev

TypeError: string indices must be integers while parsing JSON using Python?

From Dev

Python: TypeError: String indices must be integers while parsing JSON

From Dev

Python, MongoDB, Mongoengine - TypeError: string indices must be integers, not str

From Dev

Python & Selenium Click and "TypeError: string indices must be integers"

From Dev

TypeError: string indices must be integers. Python, Flask

From Dev

TypeError string indices must be integers - python json dict

From Dev

Pandas pd.merge "TypeError: string indices must be integers, not str"

From Dev

I'm getting a typeError: string indices must be integers, not type

From Dev

TypeError: string indices must be integers, not str (Rotten Tomatoes)

From Dev

Ansible: VPC redefine: TypeError: string indices must be integers, not str

From Dev

TypeError: string indices must be integers when trying to print a href

From Dev

py-corenlp - TypeError: string indices must be integers

From Dev

python pandas - TypeError when parsing JSON: string indices must be integers

From Dev

Getting mbid of a track from lastfm: TypeError: string indices must be integers

From Dev

TypeError: string indices must be integers, not str with JSON parsing

From Dev

Python 3 TypeError: string indices must be integers Error

From Dev

json.loads TypeError: string indices must be integers

From Dev

JSON String Indices Must Be Integers

From Dev

Error: String Indices must be integers

From Dev

Error: String Indices must be integers

Related Related

  1. 1

    TypeError at / string indices must be integers

  2. 2

    Python ---- TypeError: string indices must be integers

  3. 3

    zeromq: TypeError: string indices must be integers, not str

  4. 4

    Scrapy: TypeError: string indices must be integers, not str?

  5. 5

    TypeError: string indices must be integers (Python)

  6. 6

    Reading a JSON string | TypeError: string indices must be integers

  7. 7

    TypeError list indices must be integers

  8. 8

    TypeError: string indices must be integers, not str // working with dict

  9. 9

    Python and JSON Error - TypeError: string indices must be integers

  10. 10

    TypeError: string indices must be integers while parsing JSON using Python?

  11. 11

    Python: TypeError: String indices must be integers while parsing JSON

  12. 12

    Python, MongoDB, Mongoengine - TypeError: string indices must be integers, not str

  13. 13

    Python & Selenium Click and "TypeError: string indices must be integers"

  14. 14

    TypeError: string indices must be integers. Python, Flask

  15. 15

    TypeError string indices must be integers - python json dict

  16. 16

    Pandas pd.merge "TypeError: string indices must be integers, not str"

  17. 17

    I'm getting a typeError: string indices must be integers, not type

  18. 18

    TypeError: string indices must be integers, not str (Rotten Tomatoes)

  19. 19

    Ansible: VPC redefine: TypeError: string indices must be integers, not str

  20. 20

    TypeError: string indices must be integers when trying to print a href

  21. 21

    py-corenlp - TypeError: string indices must be integers

  22. 22

    python pandas - TypeError when parsing JSON: string indices must be integers

  23. 23

    Getting mbid of a track from lastfm: TypeError: string indices must be integers

  24. 24

    TypeError: string indices must be integers, not str with JSON parsing

  25. 25

    Python 3 TypeError: string indices must be integers Error

  26. 26

    json.loads TypeError: string indices must be integers

  27. 27

    JSON String Indices Must Be Integers

  28. 28

    Error: String Indices must be integers

  29. 29

    Error: String Indices must be integers

HotTag

Archive