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

" line 48"     return delta.days     ^ SyntaxError: 'return' outside function import datetime def nowDate():...
" line 48"     return delta.days     ^ SyntaxError: 'return' outside function import datetime def nowDate():     current_time = datetime.datetime.now()     new_time = datetime.datetime(         current_time.year,         current_time.month,         current_time.day,         0,         0,         0,         )     return new_time def plusOneDay(daytime):     tomorrow = daytime + datetime.timedelta(days=1)     return tomorrow def nDaystoHoliday():     NJCU_holidays = [         (datetime.date(2019, 1, 1), "New Year's Day"),         (datetime.date(2019, 1, 21), 'Martin Luther King, Jr. Day'),         (datetime.date(2019, 2, 19), "President's Day"),...
What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at...
What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at ", datetime.datetime.now()) print(getpass.getuser())
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...
Copy the following Python fuction discussed in class into your file: from random import * def...
Copy the following Python fuction discussed in class into your file: from random import * def makeRandomList(size, bound): a = [] for i in range(size): a.append(randint(0, bound)) return a a. Add another function that receives a list as a parameter and computes the sum of the elements of the list. The header of the function should be def sumList(a): The function should return the result and not print it. If the list is empty, the function should return 0. Use...
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).
Write a single python file to perform the following tasks: (a) Get dataset “from sklearn.datasets import...
Write a single python file to perform the following tasks: (a) Get dataset “from sklearn.datasets import load_iris”. Split the dataset into two sets: 20% of samples for training, and 80% of samples for testing. NOTE 1: Please use “from sklearn.model_selection import train_test_split” with “random_state=N” and “test_size=0.80”. NOTE 2: The offset/bias column is not needed here for augmenting the input features. (b) Generate the target output using one-hot encoding for both the training set and the test set. (c) Using the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT