I currently have a csv file containing a column of dates that is formatted as dd/mm/yyyy but i want to change it to yyyy/mm/dd
I have written the code below:
import csv
csv_in = open('news.csv','rb')
for rows in csv.reader(csv_in):
value = rows[0]
day= value[:2]
year= value[-4:]
month= value[3:5]
edited = year +'/'+month+'/'+day
rows[0] = edited
writer =csv.writer(open('newsedit.csv', 'wb'))
writer.writerow(rows)
for some reason the code above will only write one row and stop and i can't seem to figure out why this is happening.
Try convert the date by datetime module.
import datetime
datetime.datetime.strptime("25/01/2013", '%d/%m/%Y').strftime('%Y/%m/%d')
The strptime function loads a string to datetime object while strftime converts the datetime to another format as string.
It is because you keep initializing a new writer in each iteration. This causes that the output is being replaced over and over again. You should create a writer object only once, then you can use its writerow() method repeatedly.
(Btw. there is a nice datetime module that makes working with dates easy...)
Related
I'm running a python script and the result of it is a group of values. Let's say the result is a unique date and time.
date = now.strftime("%d/%m/%Y")
time = now.strftime("%H:%M:%S")
I would like to write down the data into xlsx file, but the problem is that the data is rewrite every single time the scrip is done.
How should I write the date to add it into the xlsx and not to rewrite the first line of it?
That's the code I am using and I'm not sure how to change it.
worksheet.write(1, 0, date)
worksheet.write(1, 1, time)
The result I would like to get at the end should be something like following:
Date Time
20/03/2022 00:24:36
20/03/2022 00:55:36
21/03/2022 15:24:36
22/03/2022 11:24:36
23/03/2022 22:24:36
You can open the excel in append mode and then keep inserting data. Refer below snippet:
with pd.ExcelWriter("existing_file_name.xlsx", engine="openpyxl", mode="a") as writer:
df.to_excel(writer, sheet_name="name")
The simplest way is to convert your data into a Pandas dataframe, then you can easily convert your dataframe into xlxs file by using a simple command like:
df.to_excel("filename.xlxs")
I'm trying to write some code that gets a CSV, but the name of that CSV has the date. For example: "myfile-07-09-2021"
I managed to get today's date file, you know how I can read the CSV day -1 (D-1)?
Use datetime.timedelta:
from datetime import timedelta
(datetime.today() - timedelta(1)).strftime('myfile-%d-%m-%Y.csv')
I am using openpyxl and pandas to generate an Excel file, and need to have dates formatted as Date in Excel. The dates in exported file are formatted correctly in dd/mm/yyyy format but when I right-click on a cell and go to 'Format Cells' it shows Custom, is there a way to change to Date? Here is my code where I specify date format.
writer = pd.ExcelWriter(dstfile, engine='openpyxl', date_format='dd/mm/yyyy')
I have also tried to set cell.number_format = 'dd/mm/yyyy' but still getting Custom format in Excel.
The answer can be found in the comments of Converting Data to Date Type When Writing with Openpyxl.
ensure you are writing a datetime.datetime object to the cell, then:
.number_format = 'mm/dd/yyyy;#' # notice the ';#'
e.g.,
import datetime
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = datetime.datetime(2021, 12, 25)
ws['A1'].number_format = 'yyyy-mm-dd;#'
wb.save(r'c:\data\test.xlsx')
n.b. these dates are still a bit 'funny' as they are not auto-magically grouped into months and years in pivot tables (if you like that sort of thing). In the pivot table, you can manually click on them and set the grouping though: https://support.microsoft.com/en-us/office/group-or-ungroup-data-in-a-pivottable-c9d1ddd0-6580-47d1-82bc-c84a5a340725
You might have to convert them to datetime objects in python if they are saved as strings in the data frame. One approach is to iterate over the cells and doing it after using ExcelWriter:
cell = datetime.strptime('30/12/1999', '%d/%m/%Y')
cell.number_format = 'dd/mm/yyyy'
A better approach is to convert that column in the data frame prior to that. You can use to_datetime function in Pandas for that.
See this answer for converting the whole column in the dataframe.
I am trying to read date column from a csv file. This column contains dates in just one format. Please see data below:
The problem arises when I am trying to read it using dateparser.
dateparse=lambda x:datetime.strptime(x, '%m/%d/%Y').date()
df = pd.read_csv('products.csv', parse_dates=['DateOfRun'], date_parser=dateparse)
Above logic works fine most of the cases, but sometimes randomly i get error that format is not matching, example below:
ValueError: time data '2020-02-23' does not match format '%m/%d/%Y'
Does anyone know how is this possible? Because that yyyy-mm-dd format is not in my data.. ANy tips will be useful.
Thanks
The problem happens when you open the csv file in Excel. Excel by default (and based on your OS settings) automatically changes the date format. For instance, in USA the default format is MM/DD/YYYY so if you have a date in a csv file such as YYYY-MM-DD it will automatically change it to MM/DD/YYYY.
The solution is to NOT open the csv file in Excel before manipulating it in Python. IF you must open it to inspect it either look at it in Python or in notepad or some other text editor.
I always assume that dates are going to be screwed up because someone might have opened it in Excel and so I test for the proper format and then change it if I get an AssertionError.
As an example if you want to change dates from YYYY-MM-DD try this:
from datetime import datetime
def change_dates(date_string):
try:
assert datetime.strptime(date_string, '%m/%d/%y'), 'format error'
return date_string
except AssertionError, ValueError:
dt = datetime.strptime(date_string, '%Y-%m-%d')
return dt.strftime('%m/%d/%Y')
#!/usr/bin/env python
# coding: utf-8
import MySQLdb
import os,sys
import time
import datetime
from pyExcelerator import *
def main():
'''get datas from mysql to excel'''
w=Workbook()
ws=w.add_sheet('user')
mysql_conn=MySQLdb.connect(................,charset="utf8")
cursor=mysql_conn.cursor()
cursor.execute("select * from students")
results=cursor.fetchall()
results_count=len(results)
cursor.close()
mysql_conn.close()
a=results_count-1
print a
#print results
row=0
for r in results:
r3=[(x[0:2],x[2],x[3:]) for x in r]
w3=datetime.strptime("%Y-%m-%d %H:%M:%S")
[ws.write(x[0:2],i) for i in r3]
[ws.write(w3,i) for i in r3]
[ws.write(x[3:],i or '') for i in r3]:
row+=1
w.save('data.xls')
if __name__ == "__main__":
main()
I want get data from mysql to excel ,but
r3=[(x[0:2],x[2],x[3:]) for x in r] gives me TypeError:'datetime.datetime' object is not subscriptable.
I do not know how to about it, and I just study only 3 week, please help me?
x is a datetime.datetime object which cannot be use with the [] notation as in x[0:2].
It means that one of your columns holds a date object which must be parsed differently.
Firstly, you don't want to be using pyExcelerator - it's old and hasn't been updated in 3 odd years (and has been superseded).
What you should be using is the utilities at http://www.python-excel.org/ and this provides functions for working with datetimes. Excel stores these as floats since a certain epoch. For info https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html - under the section "Dates in Excel spreadsheets". Also see https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#xldate.xldate_as_tuple-function for how to convert an excel representation of a date to a standard python datetime.
If you easy_install/pip xlutils, you'll get both the xlrd (reading) and xlwt (writing) libraries. Up to version 2003 files are supported, but 2007+ (.xlsx file) support is close to coming out of beta.
edit
Forgot to mention that https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/doc/xlwt.html describes how the xlwt library can take a datetime.dateime and convert that to an Excel cell.
One of the fields in your table seems to contain datetime objects, MySQLdb also returns them as datetime. You probably want to convert datetime to str first. That line seems to take some part of the datetime by using slices. You could achieve the same with datetime.strftime.