Comments (7)
I've solved the problem, change capital Y to lowercase y: '%m/%d/%Y' ----> '%m/%d/%y'!
from foundations-for-analytics-with-python.
Use '%m/%d/%Y'
when the year in the date looks like 6/15/2018
. Use '%m/%d/%y'
when the year in the date looks like 6/15/18
from foundations-for-analytics-with-python.
@HeQYT Hi, Why to do this ? Did you research the reason? I have the same issue. But in another function only '%m/%d/%Y'
can be worked.
from foundations-for-analytics-with-python.
if you add a line of 'print(str(row[column_index]))' after 'a_date = datetime.date(datetime.strptime(
str(row[column_index]), '%m/%d/%y')), you'll find out that the str function converts date format '1/20/2014' to '1/20/14'.
That's why the lower case 'y' has to be used in this case.
print is a good debugging tool for simple scripting.
See my code:
#!/usr/bin/env python3
import csv
import MySQLdb
import sys
from datetime import datetime, date
#Path to and name of a CSV input file
input_file = sys.argv[1]
#Connect to a MySQL database
con = MySQLdb.connect(host='localhost', port=3306, db='my_suppliers', user='root', passwd='root@cxzsql')
c = con.cursor()
Read the CSV file
#Insert the data into the Suppliers table
file_reader = csv.reader(open(input_file, 'r', newline=''))
header = next(file_reader)
for row in file_reader:
data = []
for column_index in range(len(header)):
if column_index < 4:
data.append(str(row[column_index]).lstrip('$')
.replace(',', '').strip())
else:
a_date = datetime.date(datetime.strptime(
str(row[column_index]), '%m/%d/%y'))
print(str(row[column_index]))
# %Y: year is 2015; %y: year is 15
a_date = a_date.strftime('%Y-%m-%d')
#print(a_date)
data.append(a_date)
#print(data)
c.execute("""INSERT INTO Suppliers VALUES (%s, %s, %s, %s, %s);""", data)
con.commit()
print("")
Query the Suppliers table
c.execute("SELECT * FROM Suppliers")
rows = c.fetchall()
for row in rows:
row_list_output = []
for column_index in range(len(row)):
row_list_output.append(str(row[column_index]))
print(row_list_output)
from foundations-for-analytics-with-python.
same the problem
from foundations-for-analytics-with-python.
Same error, the following error is occurred in some random systems even-though datetime utilities not used in the code.
Traceback (most recent call last):
File "Test.py", line 192, in
File "winapps_init_.py", line 50, in search_installed
File "winapps_init_.py", line 42, in
File "winapps_init_.py", line 40, in
File "winapps_init_.py", line 186, in installed_application
File "winapps_init.py", line 106, in
File "_strptime.py", line 568, in _strptime_datetime
File "_strptime.py", line 349, in _strptime
ValueError: time data '12/11/2019' does not match format '%Y%m%d'
from foundations-for-analytics-with-python.
you have to just remove "/" from the String and put "," in it .. Boom
Code Are Here
"import datetime as dt
str = '01,01,2017'
datetime_value = dt.datetime.strptime(str,'%d,%m,%Y')
print(datetime_value)"
#OutPut
2017-01-01 00:00:00
from foundations-for-analytics-with-python.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from foundations-for-analytics-with-python.