Read specific part of json string with python - python

I am currently working on a programme within the django environment which operates off a json api provided by a third party. There is an object within that API which I want however the string of information it provides is too much for me.
The data I want is the created_at tag from the twitter api using tweepy. This created_at contains data in the following format:
"created_at": "Mon Aug 27 17:21:03 +0000 2012"
This is all fine however this will return the date AND time whereas I simply want the the time part of the above example i.e. 17:21:03.
Is there any way I can just take this part of the created_at response string and store it in a separate variable?

You can use the dateutil module
from dateutil import parser
created_at = "Mon Aug 27 17:21:03 +0000 2012"
created_at = parser.parse(created_at)
print created_at.time()
Output:
17:21:03

Try below code.
my_datetime = response_from_twitter['created_at']
my_time = my_datetime.split(' ')[3]
# my_time will now contain time part.

You could just split the string into a list and take the 4th element:
time = source['created_at'].split(' ')[3]

What about a regular expression with re.search():
>>> import re
>>> d = {"created_at": "Mon Aug 27 17:21:03 +0000 2012"}
>>> re.search('\d{2}:\d{2}:\d{2}', d['created_at']).group(0)
'17:21:03'

Related

Parse timestamp from a txt file?

I have a large text file on the web that I am using requests to obtain and parse data from. The text file begins each line with a format like [Mon Oct 10 08:58:26 2022]. How can I get the latest 7 days or convert only the datetime to an object or string for storing and parsing later? I simply want to extract the timestamps from the log and print them
You can use TimedRotatingFileHandler for daily or 7-days logs.
read more about timed rotating file handler here
and
read more about extracting timestamps from files
Can you tell me if this snippet solves your problem?
from datetime import datetime
log_line = "[Sun Oct 09 06:14:26 2022] Wiladoc is browsing your wares."
_datetime = log_line[1:25]
_datetime_strp = datetime.strptime(_datetime, '%a %b %d %H:%M:%S %Y')
print(_datetime)
print(_datetime_strp)

which the best way to get some data in text below

Without modules xml or lxml,
with module re
or maybe another way,
how best to get the data between the quotes: https://10.107.11.77:52311/api/computer/1624350712
my option with re:
found = re.findall(r'<Computer Resource=\s*"([^"]*)"', r.text)
Text:
<Computer Resource="https://10.107.11.77:52311/api/computer/1624350712">
<LastReportTime>Thu, 13 May 2021 22:59:43 +0000</LastReportTime>
<ID>1624350712</ID>
</Computer>
<Computer Resource="https://10.107.11.77:52311/api/computer/1626165598">
<LastReportTime>Wed, 02 Jun 2021 07:12:40 +0000</LastReportTime>
<ID>1626165598</ID>
my option with re: found = re.findall(r'<Computer Resource=\s*"([^"]*)"', r.text)

JSON to String in python

there is part in my project using python where I get a JSON object and get those values and convert them to string.
My input will be:
data = {
"date":'12.04.2019',
"time":'06 am',
"event":'Complete project'
}
My output should be:
On 12.04.2019, 06 am the reminder is to Complete project
I have tried different methods to convert the JSON values to String but I get some sort of errors.
this's not a Json object, this is a dictionary so you can direct access the keys in it i.e.
res="On "+str(data['date'])+", "+str(data['time'])+"the reminder is to"+str(data['event'])
print(res)
you can also use date.get('date') to get the value of a specific key,they're both the same
for more info about dictionaries :https://www.w3schools.com/python/python_dictionaries.asp
for more info about Json Objects: https://www.w3schools.com/python/python_json.asp
As of python 3.6 you can use f strings:
print(f"On {data['date']}, {data['time']} the reminder is to {data['event']}")
If you don't have python 3.6 or above I suggest you to use formatting:
print("On %s, %s the reminder is to %s" %(data['date'], data['time'], data['event']))
data is not a JSON object it is a dictionary, so you can extract data using a keys.
Sample code:
data = {
"date":'12.04.2019',
"time":'06 am',
"event":'Complete project'
}
print("On "+ data["date"]+","+ data["time"] +" the reminder is to "+ data["event"])
Output:
On 12.04.2019,06 am the reminder is to Complete project

Python Twitter API TwitterError: user_id must be type int

I'm trying to use python-twitter. I'm following this tutorial.
Here is my code:
tweet = cache.get('tweet')
if not tweet:
tweet = twitter.Api().GetUserTimeline(settings.TWITTER_USER)[0]
tweet.date = datetime.strptime(tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y")
cache.set('tweet', tweet, settings.TWITTER_TIMEOUT)
But I'm getting next error:
Twitter.error.TwitterError: {'message': '"user_id" must be type int'}
I've set the variables like in the tutorial:
TWITTER_USER = 'CaseyNeistat'
TWITTER_TIMEOUT = 3600
Is there anything I'm doing wrong?
The article got created about 9 years ago. So the API could changed many times since then.
Here is the signature of the function from python-twitter documentation:
GetUserTimeline(user_id=None, screen_name=None, since_id=None, max_id=None, count=None, include_rts=True, trim_user=False, exclude_replies=False)
So you want to do GetUserTimeline(screen_name=settings.TWITTER_USER)
Otherwise your string would go as user_id.
Just use screen_name as function argument.
For example, GetUserTimeline(screen_name="elonmusk")

Django Date Input Parsing?

I'm trying to get a date for an event from a user.
The input is just a simple html text input.
My main problem is that I don't know how to parse the date.
If I try to pass the raw string, I get a TypeError, as expected.
Does Django have any date-parsing modules?
If you are using django.forms look at DateField.input_formats. This argument allows to define several date formats. DateField tries to parse raw data according to those formats in order.
Django doesn't, so to speak, by Python does. It seems I'm wrong here, as uptimebox's answer shows.
Say you're parsing this string: 'Wed Apr 21 19:29:07 +0000 2010' (This is from Twitter's JSON API)
You'd parse it into a datetime object like this:
import datetime
JSON_time = 'Wed Apr 21 19:29:07 +0000 2010'
my_time = datetime.datetime.strptime(JSON_time, '%a %b %d %H:%M:%S +0000 %Y')
print type(my_time)
You'd get this, confirming it is a datetime object:
<type 'datetime.datetime'>
More information on strptime() can be found here.
(In 2017), you could now use django.utils.dateparse
The DateField can be used outside of Django forms.
Example, when used {{ value|date:"SHORT_DATE_FORMAT" }} in template:
from django.forms import DateField
from django.utils import formats
# need '%d.%m.%Y' instead of 'd.m.Y' from get_format()
dformat = ('.' + formats.get_format("SHORT_DATE_FORMAT", lang=request.LANGUAGE_CODE)).replace('.', '.%').replace('-', '-%').replace('/', '/%')[1:]
dfield = DateField(input_formats=(dformat,))
<date> = dfield.to_python(<string>)

Categories

Resources