I want to remove special character '-' from date format in python. I have retrieved maximum date from a database column.
Here is my small code:
def max_date():
Max_Date= hive_select('SELECT MAX (t_date) FROM ovisto.ovisto_log')
value = Max_Date[0]
print value
Here is output:
{0: '2017-02-21', '_c0': '2017-02-21'}
I want only numbers without special character '-' from output.
so, I am expecting this answer '20170221'
I have tried in different ways but could not get proper answer.
How can I get in a simple way? Thanks for your time.
just rebuild a new dictionary using dict comprehension, iterating on the original dictionary, and stripping the unwanted characters from values using str.replace
d = {0: '2017-02-21', '_c0': '2017-02-21'}
new_d = {k:v.replace("-","") for k,v in d.items()}
print(new_d)
result:
{0: '20170221', '_c0': '20170221'}
if you only want to keep the values and drop the duplicates (and the order too :), use a set comprehension with the values instead:
s = {v.replace("-","") for _,v in d.items()}
You can try strptime:
value = Max_Date[0]
new_val= datetime.datetime.strptime( str( value ), '%Y%m%d').strftime('%m/%d/%y')
I found it here: How to convert a date string to different format
Related
The string value
value = "[new=user,pass=h[]#,repalce=que]"
I need the output to be as a list
list = [ "new=user","pass=h[]#","repalce=que" ]
I am new to python just want to know how to deal with these type convertions
You can use the split method as follows:
value = "[new=user,pass=h[]#,repalce=que]"
splitList = value.split(",")
print(splitList)
The split method takes the character that you want to split the sentence on as a parameter.
You can do that in this particular case by the following command
value_list = value[1:-1].split(',')
This way you will first get rid of the brackets (first and last character) and then split by the comma. I do not think there is a more elegant way since this is a rather rare case. I would recommend loading lists as lists or other variables in their correct format and avoid changing the type.
you can use str.split
value = "[new=user,pass=h[]#,repalce=que]"
result = value.split(',')
please note that list is a type. I recommend not to use it as a variable name.
You could do it using a list comprehension like this:
value = "[new=user,pass=h[]#,repalce=que]"
new_value = [t.replace('pass', 'test') for t in value[1:-1].split(',')]
print(new_value)
Output:
['new=user', 'test=h[]#', 'repalce=que']
Note:
This only works for this particular case.
Also, there's no "type conversion" here
Clean the string thyen convert string to list by string.split()
value = "[new=user,pass=h[]#,repalce=que]"
value = value [1:-1].split(",")
print(value)
output #
['new=user', 'pass=h[]#', 'repalce=que']
I assume the []# is not supposed to be taken as list. So this is my solution, maybe it would be better to use regex but whatever.
value = "[new=user,pass=h[]#,repalce=que]"
def func(x):
n=len(x)
x=list(x)
s=e=-1
for i in range(0,n-2,1):
if x[i]=='[' and x[i+1]!=']' and x[i+2]!='#':s=i
if x[i]==']' and x[i-1]!='[' and x[i+1]!='#':e=i
x[s]=x[e]=''
x=''.join(x)
x=x.split(',')
return x
print(func(value))
You could split the string
# Python code to convert string to list
def Convert(string):
li = list(string.split(" "))
return li
str1 = "Item1 Item2 Item3"
print(Convert(str1))
Output:
['Item1', 'Item2', 'Item3']
I'm just practicing basic web scraping using Python and Regex
I want to write a function that takes a string object as input and returns a dictionary where each key is a date as string like '2017-01-23' (without the quotes tho); and each value corresponding is the approval rating, stored as a floating numbers.
Here is what the input object(data) looks like:
As you can see, each record(per day) is denoted by {}, and each key:value pattern followed by ','
{"date":"2017-01-23","future":false,"subgroup":"All polls","approve_estimate":"45.46693",
"approve_hi":"50.88971","approve_lo":"40.04416","disapprove_estimate":"41.26452",
"disapprove_hi":"46.68729","disapprove_lo":"35.84175"},
{"date":"2017-01-24","future":false,"subgroup":"All polls"
...................
Here's a regex pattern for the dates:
date_pattern = r'\d{4}-\d{2}-\d{2}'
Using this,
date_pattern = r'\d{4}-\d{2}-\d{2}'
date_matcher = re.compile(date_pattern)
date_matches = matcher.findall(long_string) #list of all dates in string
But for the actual approval rating value, this wouldn't work because I'm not looking for a match, but the number that comes after this, which is 45.46693 in this example.
approve_pattern = r'approve_estimate\":'
#float(re.sub('[aZ]','',re.sub('["]','',re.split(approve_pattern, data) [1])))
The problem with the approve_pattern is that I can only fetch one value at a time. So how can I do this for the entire data and store the approve rating values as float?
Also, I want to only keep records for which "future":false to discard predicted values, and only keep the values with "future":true.
Please assume all encountered dates have valid approval estimates.
Here's the desired output
date_matches=['2018-01-01','2018-01-02','2018-01-03'] # "future":true filtered out
approve_matches=[47.1,47.2,47.9]
final_dict = {k:v for k,v in zip(date_matches,approve_matches)}
final_dict #Desired Output {'2018-01-01': 47.1, '2018-01-02': 47.2, '2018-01-03': 47.9}
Your data looks very much like JSON, except that it must be enclosed in brackets to form an array. You should use a JSON parser (e.g., json.loads) to read it.
Let's say s is your original string. Then the following expression results in your dictionary:
final_dict = {record['date']: record['approve_estimate']
for record in json.loads("[" + s + "]")
if record['future']}
# Empty in your case
I am parsing a large text file that has key value pairs separated by '='. I need to split these key value pairs into a dictionary. I was simply going to split by '='. However I noticed that some of the values contain the equals sign character. When a value contains the equals sign character, it seems to be always wrapped in parenthesis.
Question: How can I split by equals sign only when the equals sign is not in between two parenthesis?
Example data:
PowSup=PS1(type=Emerson,fw=v.03.05.00)
Desired output:
{'PowSup': 'PS1(type=Emerson,fw=v.03.05.00)'}
UPDATE: The data does not seem to have any nested parenthesis. (Hopefully that remains true in the future)
UPDATE 2: The key doesn't ever seem to have equals sign either.
UPDATE 3: The full requirements are much more complicated and at this point I am stuck so I have opened up a new question here: Python parse output of mixed format text file to key value pair dictionaries
You could try partition('=') to split from the first instance
'PowSup=PS1(type=Emerson,fw=v.03.05.00)'.partition('=')[0:3:2]
mydict=dict()
for line in file:
k,v=line.split('=',1)
mydict[k]=v
Simple solution using str.index() function:
s = "PowSup=PS1(type=Emerson,fw=v.03.05.00)"
pos = s.index('=') # detecting the first position of `=` character
print {s[:pos]:s[pos+1:]}
The output:
{'PowSup': 'PS1(type=Emerson,fw=v.03.05.00)'}
You can limit the split() operation to a single split (the first =):
>>> x = "PowSup=PS1(type=Emerson,fw=v.03.05.00)"
>>> x.split('=', 1)
['PowSup', 'PS1(type=Emerson,fw=v.03.05.00)']
You can then use these values to populate your dict:
>>> x = "PowSup=PS1(type=Emerson,fw=v.03.05.00)"
>>> key, value = x.split('=', 1)
>>> out = {}
>>> out[key] = value
>>> out
{'PowSup': 'PS1(type=Emerson,fw=v.03.05.00)'}
I have a list of this format:
["['05-Aug-13 10:17', '05-Aug-13 10:17', '05-Aug-13 15:17']"]
I am using:
for d in date:
print d
This produces:
['05-Aug-13 10:17', '05-Aug-13 10:17', '05-Aug-13 15:17']
I then try and add this to a defaultdict, so underneath the print d I write:
myDict[text].append(date)
Later, I try and iterate through this by using:
for text, date in myDict.iteritems():
for d in date:
print d, '\n'
But this doesn't work, just producing the format show in the first line of code in this question. The format suggests a list in a list, so I tried using:
for d in date:
for e in d:
myDict[text].append(e)
But this included every character of the line, as opposed to each separate date. What am I doing wrong? How can I have a defaultdict with this format
text : ['05-Aug-13 10:17', '06-Aug-13 11:17']
whose values can all be reached?
Your list contains only one element: the string representation of another list. You will have to parse it into an actual list before treating it like one:
import ast
actual_list = ast.literal_eval(your_list[0])
As an alternative (though the regular expression might need tuning for your use)
import re
pattern = r"\d{2}-[A-Z][a-z]{2}-\d{1,2} \d{2}:\d{2}"
re.findall(ptrn, your_list[0])
I have following string
adId:4028cb901dd9720a011e1160afbc01a3;siteId:8a8ee4f720e6beb70120e6d8e08b0002;userId:5082a05c-015e-4266-9874-5dc6262da3e0
I need only the value of adId,siteId and userId.
means
4028cb901dd9720a011e1160afbc01a3
8a8ee4f720e6beb70120e6d8e08b0002
5082a05c-015e-4266-9874-5dc6262da3e0
all the 3 in different variable or in a array so that i can use all three
You can split them to a dictionary if you don't need any fancy parsing:
In [2]: dict(kvpair.split(':') for kvpair in s.split(';'))
Out[2]:
{'adId': '4028cb901dd9720a011e1160afbc01a3',
'siteId': '8a8ee4f720e6beb70120e6d8e08b0002',
'userId': '5082a05c-015e-4266-9874-5dc6262da3e0'}
You could do something like this:
input='adId:4028cb901dd9720a011e1160afbc01a3;siteId:8a8ee4f720e6beb70120e6d8e08b0002;userId:5082a05c-015e-4266-9874-5dc6262da3e0'
result={}
for pair in input.split(';'):
(key,value) = pair.split(':')
result[key] = value
print result['adId']
print result['siteId']
print result['userId']
matches = re.findall("([a-z0-9A-Z_]+):([a-zA-Z0-9\-]+);", buf)
for m in matches:
#m[1] is adid and things
#m[2] is the long string.
You can also limit the lengths using {32} like
([a-zA-Z0-9]+){32};
Regular expressions allow you to validate the string and split it into component parts.
There is an awesome method called split() for python that will work nicely for you. I would suggest using it twice, once for ';' then again for each one of those using ':'.