capture/parsing entries by date range [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
It is possible to capture/parsing entries with 3 days old from current date using standard module "re"?
Below sample file from where I want capture entries.
xx: xxxxxx ; xxxx: 2017-09-2T14:13:17 ; xxxxxxxx: xxxxxxxxxxxxxxxxxxxxxxxxxx

You have to extract with the re module the string with the hour and then:
from datetime import datetime
capturedEntries = []
THREE_DAYS = datetime.timedelta(3)
# I suppose you'are reading lines in another way, but as an example...
for entry in entries:
# I'm assuming each entry follows the format
# xx: xxxxxx ; xxxx: 2017-09-2T14:13:17 ; xxxxxxxx: xxxxxxxxxxxxxxxxxxxxxxxxxx
stringDate = re.search(r'\d{4}-\d{1,2}-\d{1,2}T\d{2}:\d{2}:\d{2}', line)
if stringDate.group():
parsedDate = datetime.strptime(stringDate.group(), '%Y-%m-%dT%H:%M:%S')
timeFromDate = (datetime.today() - parsedDate)
if timeFromDate > THREE_DAYS:
# We save only the datetimes you asked for in datetime format
# For string format, replace parsedDate for stringDate
capturedEntries.append(parsedDate)

Related

How do I remove certain parts from a string in a dataset in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have a data set that has a code and then a name with a code, and there multiple codes and multiple names example:
|CODE|NAME|
|TN |Tobey JacobsTN|
|GD |Lars OwensGD|
|YO |Mark SmithYO|
|BM |John SawyerBM|
etc...
How would I take the code out and just leave the name using python?
You could do something like this:
strings = [
'|CODE|NAME|',
'|TN |Tobey JacobsTN|',
'|GD |Lars OwensGD|',
'|YO |Mark SmithYO|',
'|BM |John SawyerBM|'
]
records = [[field.rstrip(' ') for field in s.split('|')[1:-1]] for s in strings][1:]
print("records:"); [print(x) for x in records]
names = [name[:-len(code)] for code, name in records]
print("names:"); [print(x) for x in names]
... which gives the following output:
records:
['TN', 'Tobey JacobsTN']
['GD', 'Lars OwensGD']
['YO', 'Mark SmithYO']
['BM', 'John SawyerBM']
names:
Tobey Jacobs
Lars Owens
Mark Smith
John Sawyer

Extract a string from within a tag Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
'''script type="7b065748e865a1f7340d2b51-text/javascript">
$.ajaxSetup({cache: false});
//Load all the variables
var player = [];
var playerid = 0;
var mode = "none";
var ep_identifier = "dvPrKEDsjzz7SJdbqRWHwfh1juZlbFX09EGP21HVhh0dqbsen3XrP2nAav4sV8mwTHJjcgUyqeluWuBr4Rf8tzbfgj9jr39weaK70pVjuSqLJgHRyS+DQuwU79srG66hpISzJkK2DjvnnjDi7qizoqwCqLhRmVIdHx930uU8n+Ab08KScv3HhJf9L7B+Y8ALzGjJCjk8ce11Izruz6CdkpbtCQZo8/OrSHqLw6UKjhwlEV/tBJlpz1jcXf7V6S2N";
var VidStreaming = "https://gogo-play.net/load.php?id=MTU4MjAz&title=Jouran%3A+The+Princess+of+Snow+and+Blood&typesub=SUB&sub=&cover=Y292ZXIvam91cmFuLXRoZS1wcmluY2Vzcy1vZi1zbm93LWFuZC1ibG9vZC5wbmc=";
var VidHLS = "";'''
**How do i extract "https://gogo-play.net/load.php?id=MTU4MjAz&title=Jouran%3A+The+Princess+of+Snow+and+Blood&typesub=SUB&sub=&cover=Y292ZXIvam91cmFuLXRoZS1wcmluY2Vzcy1vZi1zbm93LWFuZC1ibG9vZC5wbmc=""
if what you mean is the link then you can use Regex with this syntax
"(https://.+)"
for example:
import re
result = re.findall(r" '(https://.+)' ", the_string_to_extract_from)
to extract it with 2 conditions:
the start of the link is https://
the link is contained in " "
You may want to provide more information on this problem.

How to loop through a csv file and only when it encountered a specific word save it into a list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to loop through this csv that has country, data, and a number I need to extract. The file looks like this:
b'/O_o/\ngoogle.visualization.Query.setResponse({"version":"0.6","reqId":"0output=csv","status":"ok","sig":"1241529276","table":{"cols":[{"id":"A","label":"Entity","type":"string"},{"id":"B","label":"Week","type":"number","pattern":"General"},{"id":"C","label":"Day","type":"date","pattern":"yyyy-mm-dd"},{"id":"D","label":"Flights
2019
(Reference)","type":"number","pattern":"General"},{"id":"E","label":"Flights","type":"number","pattern":"General"},{"id":"F","label":"%
vs 2019
(Daily)","type":"number","pattern":"General"},{"id":"G","label":"Flights
(7-day moving
average)","type":"number","pattern":"General"},{"id":"H","label":"% vs
2019 (7-day Moving
Average)","type":"number","pattern":"General"},{"id":"I","label":"Day
2019","type":"date","pattern":"yyyy-mm-dd"},{"id":"J","label":"Day
Previous
Year","type":"date","pattern":"yyyy-mm-dd"},{"id":"K","label":"Flights
Previous
Year","type":"number","pattern":"General"}],"rows":[{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,1)","f":"2020-09-01"},{"v":129.0,"f":"129"},{"v":64.0,"f":"64"},{"v":-0.503875968992248,"f":"-0,503875969"},{"v":71.5714285714286,"f":"71,57142857"},{"v":-0.291371994342291,"f":"-0,2913719943"},{"v":"Date(2019,8,3)","f":"2019-09-03"},{"v":"Date(2019,8,3)","f":"2019-09-03"},{"v":129.0,"f":"129"}]},{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,2)","f":"2020-09-02"},{"v":92.0,"f":"92"},{"v":59.0,"f":"59"},{"v":-0.358695652173913,"f":"-0,3586956522"},{"v":70.0,"f":"70"},{"v":-0.300998573466476,"f":"-0,3009985735"},{"v":"Date(2019,8,4)","f":"2019-09-04"},{"v":"Date(2019,8,4)","f":"2019-09-04"},{"v":92.0,"f":"92"}]},{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,3)","f":"2020-09-03"},{"v":96.0,"f":"96"},{"v":67.0,"f":"67"},{"v":-0.302083333333333,"f":"-0,3020833333"},{"v":70.1428571428571,"f":"70,14285714"},{"v":-0.30354609929078,"f":"-0,3035460993"},{"v":"Date(2019,8,5)","f":"2019-09-05"},{"v":"Date(2019,8,5)","f":"2019-09-05"},{"v":96.0,"f":"96"}]},{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,4)","f":"2020-09-04"},{"v":103.0,"f":"103"},{"v":89.0,"f":"89"},{"v":-0.135922330097087,"f":"-0,1359223301"},{"v":69.2857142857143,"f":"69,28571429"},{"v":-0.312056737588652,"f":"-0,3120567376"},{"v":"Date(2019,8,6)","f":"2019-09-06"},{"v":"Date(2019,8,6)","f":"2019-09-06"},{"v":103.0,"f":"103"}]},{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,5)","f":"2020-09-05"},{"v":94.0,"f":"94"},{"v":53.0,"f":"53"},{"v":-0.436170212765957,"f":"-0,4361702128"},{"v":68.8571428571429,"f":"68,85714286"},{"v":-0.314366998577525,"f":"-0,3143669986"},{"v":"Date(2019,8,7)","f":"2019-09-07"},{"v":"Date(2019,8,7)","f":"2019-09-07"},{"v":94.0,"f":"94"}]}, ...
In there it says Albania, which is a country with data I need to extract. For example:
{"c":[{"v":"Albania"},{"v":36.0,"f":"36"},{"v":"Date(2020,8,4)","f":"2020-09-04"},{"v":103.0,"f":"103"},{"v":89.0,"f":"89"},{"v":-0.135922330097087,"f":"-0,1359223301"},{"v":69.2857142857143,"f":"69,28571429"},{"v":-0.312056737588652,"f":"-0,3120567376"},{"v":"Date(2019,8,6)","f":"2019-09-06"},{"v":"Date(2019,8,6)","f":"2019-09-06"},{"v":103.0,"f":"103"}]}
How would I write a python script to loop over the entire csv file and find every occurrence of the word "Albania", save it, then go a little further and get the date "2020-09-04", and then get the number -0.1359?
You can read this using json.
import json
with open('txtfile1.txt') as input:
data = input.read()
#Select the part where the dictionary start (after "rows":)
nesteddict = json.loads(data[(data.find('rows')+6):])
finallist = []
for x in nesteddict:
sublist = []
sublist.append(x['c'][0]['v'])
sublist.append(x['c'][2]['f'])
sublist.append(x['c'][5]['v'])
finallist.append(sublist)
This will output
[['Albania', '2020-09-01', -0.503875968992248], ['Albania', '2020-09-02', -0.358695652173913], ['Albania', '2020-09-03', -0.302083333333333], ['Albania', '2020-09-04', -0.135922330097087], ['Albania', '2020-09-05', -0.436170212765957]]

Why am I getting this error IndexError: tuple index out of range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to pull rows from a database. It pulls the first two rows however will not pull out rows 6 and 7, and it's saying this error: tuple index out of range.
db = sqlite3.connect('da_destination_history')
cursor = db.cursor()
cursor.execute('''SELECT time, dest_lat, dest_lng, source_lat, source_lng FROM destination_history''')
rows = cursor.fetchall()
for row in rows:
print('{0} : {1}, {2}'.format(row[0], float(row[1])/1000000, float(row[2])/1000000))
print('{0} : {6}, {7}'.format(row[0], float(row[6])/1000000, float(row[7])/1000000))
ttime = row[0]
dlong = float(row[1])/1000000
dlat = float(row[2])/1000000
kml = simplekml.Kml()
kml.newpoint(name="andriod2", coords=[((float(row[2])/1000000),(float(row[1])/1000000))])
kml.newpoint(name="andriod", coords=[((float(row[7])/1000000),(float(row[6])/1000000))])
kml.save("andriod.kml")
As Martijn indicated the indexing of row[6] looks suspicious as you only select 5 columns.
There is also a problem with this format function on the string:
'{0} : {6}, {7}'.format(row[0], float(row[6])/1000000, float(row[7])/1000000)
as 6 and 7 between {} indicate the argument to format() and you provide only 3 arguments (indexed with {0}, {1} resp. {2}
If you copied this code from some example with more columns selected, you should make sure you update all indices, both in row, as well as in the format instructions.

How to use data from csv file in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My current gradesheetscores.csv file looks like this
Name ,Score
Alice ,56
Jack,90
Naima,56
Marcus,87
Lizzy,34
Yasmin,20
TOP,100
Hannah,78
Rosa,32
Jaesuk,100
I want to use the scores only for my program to get average and other stuff (which I can do)I only need to know - How do I select only these data ? I'm a beginner (2 weeks old) so don't be harsh please.
you can use csv package
import csv
data_file = open('your_file.csv', 'rb')
reader = csv.reader(data_file)
column = {}
headers = reader.next()
for h in headers:
column[h] = []
for row in reader:
for h, v in zip(headers, row):
column[h].append(v)
score = column['Score']
now score variable contains your score values

Categories

Resources