I don't run my code, it's an example
if request.method == "POST": form = XXXXForm(request.POST, request.FILES) if form.is_valid(): docfile = request.FILES['docfile'] if isinstance(docfile, (InMemoryUploadedFile, TemporaryUploadedFile)): book = xlrd.open_workbook(file_contents = docfile.read(), formatting_info = True, on_demand = True) else: book = xlrd.open_workbook(filename = docfile, formatting_info = True, on_demand = True) sheet = book.sheet_by_index(0) new_csv_file = csv.writer(open('new_filename', 'w')) # read file for line in range(1, sheet.nrows): new_row = [request.session.get('uploader'), request.session.get('date')] new_row.extend(sheet.row_values(line)) new_csv_file.writerow(new_row) ...
if request.method == "POST":
form = XXXXForm(request.POST,request.FILES)
if form.is_valid():
docfile=request.FILES['docfile']
if isinstance(docfile, (InMemoryUploadedFile, TemporaryUploadedFile)):
book = xlrd.open_workbook(file_contents = docfile.read(), formatting_info = True, on_demand = True)
else:
book = xlrd.open_workbook(filename = docfile, formatting_info = True, on_demand = True)
sheet = book.sheet_by_index(0)
new_csv_file = csv.writer(open('new_filename', 'w'))
# read file
for line in range(1, sheet.nrows):
new_row = [request.session.get('uploader'), request.session.get('date')]
new_row.extend(sheet.row_values(line))
new_csv_file.writerow(new_row)
if request.method == "POST":
form = XXXXForm(request.POST,request.FILES)
if form.is_valid():
docfile=request.FILES['docfile']
if isinstance(docfile, (InMemoryUploadedFile, TemporaryUploadedFile)):
book = xlrd.open_workbook(file_contents = docfile.read(), formatting_info = True, on_demand = True)
else:
book = xlrd.open_workbook(filename = docfile, formatting_info = True, on_demand = True)
sheet = book.sheet_by_index(0)
new_csv_file = csv.writer(open('new_filename', 'w'))
# read file
for line in range(1, sheet.nrows):
new_row = [request.session.get('uploader'), request.session.get('date')]
new_row.extend(sheet.row_values(line))
new_csv_file.writerow(new_row)
django1.8- how to append information manually when uploading Excel and inserting into database,django1.8- concatenate fields in Excel and upload in database,How can i doing for filtering for two fields in database in django and MySQL?,Django how to upload CSV file using Form to populate postgres database and display all items in browser
You can use str.format
:
>>> year_var = 2015 >>>
month_var = 12 >>>
'{}-{}-{}'.format(year_var, month_var, '01')
'2015-12-01'
>>> '%s-%s-%s' % (year_var, month_var, '01')
'2015-12-01'
The values could be passed as float values: 2015.0
, 3.0
>>> year_var = 2015.0 >>>
month_var = 3.0 >>>
'{:04.0f}-{:02.0f}-{}'.format(year_var, month_var, '01')
'2015-03-01'
>>>
'%.0f-%02.0f-%s' % (year_var, month_var, '01')
'2015-03-01'
The following is an example walkthrough of importing an Excel document into a MySQL database. To run this tutorial you will need an Excel file, and admin access to a running MySQL instance.,For the example we’ll be using the following Excel file on rental boats:,Log into your MySQL shell and create a database. For this example the database will be named boatdb. Note that the --local-infile option is needed by some versions of MySQL for the data loading we’ll do in the following steps. $ mysql -u root -p --local-infile mysql> create database boatdb; mysql> use boatdb; ,Open your Excel file and click Save As. Choose to save it as a .CSV (Comma Separated) file. If you are running Excel on a Mac, you will need to save the file as a Windows Comma Separated (.csv) or CSV (Windows) to maintain the correct formatting.
$ mysql - u root - p--local - infile mysql > create database boatdb; mysql > use boatdb;
CREATE TABLE boats(
d INT NOT NULL PRIMARY KEY,
name VARCHAR(40),
type VARCHAR(10),
owner_id INT NOT NULL,
date_made DATE,
rental_price FLOAT
);
mysql > show tables; + -- -- -- -- -- -- -- -- -- + | Tables_in_boatdb | + -- -- -- -- -- -- -- -- -- + | boats | + -- -- -- -- -- -- -- -- -- +
LOAD DATA LOCAL INFILE "/path/to/boats.csv"
INTO TABLE boatdb.boats
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id, name, type, owner_id, @datevar, rental_price)
set date_made = STR_TO_DATE(@datevar, '%m/%d/%Y');