Question

In: Computer Science

#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()...

#Python 3.7 "Has no attribute" error -

def get():
    import datetime
    d = date_time_obj.date()
    return(d)

print(a["Date"])
print("3/14/2012".get())

How to write the "get" function (without any imput), to convery the format ""3/14/2012" to the format "2012-03-14", by simply using "3/14/2012".get() ?

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Solution 1 :Without Import datetime

Here a new python program with name "main.py" is created, which contains following code.

main.py :

#function
def get(date):
#split date based on /
date=date.split("/")
#concate elements from list
date=date[2]+"-"+str(0)+date[0]+"-"+date[1]
return date #retun date
#function call to print date
print(get("3/14/2012"))

======================================================

Output : Compile and Run main.py to get the screen as shown below

Screen 1 :main.py

********************

Solution 2 :With import datetime

main.py :

import datetime #import
#pass date and date format
d = datetime.datetime.strptime("3/14/2012", '%m/%d/%Y')
#print date format
print(datetime.date.strftime(d, "%Y-%m-%d"))

===========================

Screen 1 :

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

USING PYTHON 3.7: import Student as st import random THRESHOLD = 0.01 tests = 0 passed...
USING PYTHON 3.7: import Student as st import random THRESHOLD = 0.01 tests = 0 passed = 0 def main():    test_get_higher_grade_student()    test_is_grade_above()    test_get_students()    test_get_classlist()    # test_count_above()    # test_get_average_grade()    print("TEST RESULTS:", passed, "/", tests) def test_get_higher_grade_student():    s1a = st.Student("V0002000", 89)    s1b = st.Student("V0002001", 89)    s2 = st.Student("V0002002", 67)    s3 = st.Student("V0002002", 97)    result = get_higher_grade_student(s1a,s1b)    print_test("get_higher_grade_student(s1a,s1b)", result == s1a)    result = get_higher_grade_student(s1a,s2)    print_test("get_higher_grade_student(s1a,s1b)", result == s1a)...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path):...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path): """ Retrieve list of students and their expected grade from file, generate a sampled test grade for each student drawn from a Gaussian distribution defined by the student expected grade as mean, and the given standard deviation. If the sample is higher than 100, re-sample. If the sample is lower than 0 or 5 standard deviations below mean, re-sample Write the list of student...
PYTHON PROBLEM: TASKED TO FIND THE FLAW WITH THE FOLLOWING CODE: from itertools import count def...
PYTHON PROBLEM: TASKED TO FIND THE FLAW WITH THE FOLLOWING CODE: from itertools import count def take_e(n, gen):     return [elem for (i, elem) in enumerate(gen) if i < n] def take_zc(n, gen):     return [elem for (i, elem) in zip(count(), gen) if i < n] FIBONACCI UNBOUNDED: def fibonacci_unbounded():     """     Unbounded Fibonacci numbers generator     """     (a, b) = (0, 1)     while True:         # return a         yield a         (a, b) = (b, a + b) SUPPOSED TO RUN THIS TO ASSIST WITH...
Please fix all the errors in this Python program. import math def solve(a, b, c): """...
Please fix all the errors in this Python program. import math def solve(a, b, c): """ Calculate solution to quadratic equation and return @param coefficients a,b,class @return either 2 roots, 1 root, or None """ #@TODO - Fix this code to handle special cases d = b ** 2 - 4 * a * c disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 if...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
Python I am creating a class in python. Here is my code below: import csv import...
Python I am creating a class in python. Here is my code below: import csv import json population = list() with open('PopChange.csv', 'r') as p: reader = csv.reader(p) next(reader) for line in reader: population.append(obj.POP(line)) population.append(obj.POP(line)) class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 = self.data[4].strip() self.popJul1 = self.data[5].strip() self.changePop = self.data[6].strip() The problem is, I get an error saying:  ...
Python Please Define a class that has one data attribute of type float. The initializer does...
Python Please Define a class that has one data attribute of type float. The initializer does not accept an argument, however, it initializes the data attribute to zero. Include in class the following methods: 1. func that will accept as two arguments. The first argument a value of float type and the second argument if string. The function will perform the following depending on the string argument: a. if string is '+', the first argument will be added to the...
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2...
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2 topBound = win.window_height() / 2 bottomBound = -win.window_height() / 2 turtleX = turt.xcor() turtleY = turt.ycor() stillIn = True if turtleX > rightBound or turtleX < leftBound: stillIn = False if turtleY > topBound or turtleY < bottomBound: stillIn = False return stillIn def main(): wn = turtle.Screen() # Define your turtles here june = turtle.Turtle() june.shape('turtle') while isInScreen(wn,june): coin = random.randrange(0, 2) if...
For python... class car:    def __init__(self)          self.tire          self.gas    def truck(self,color)        &nb
For python... class car:    def __init__(self)          self.tire          self.gas    def truck(self,color)               style = color                return    def plane(self,oil)              liquid = self.oil + self.truck(color) For the plane method, I am getting an error that the class does not define __add__ inside init so I cannot use the + operator. How do I go about this.             
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT