How to split date and format it - python

I already figured out how to split the name from the date from a imported text file. I am trying to figure out how I could change the date from a mm/dd/yyyy format into month, date, yyyy.
This is the code I have to separate the names from the date:
with open("dates.txt",'r') as data_file: for line in data_file: data = line.split(',')
would I have to do an import such as datetime or pandas?
and here is how the first line of the text file looks like:
Richard Alexander,12/21/1995

import datetime
def format_to_format(raw_date: str, source_format: str, target_format: str) -> str:
"""
:raw_date: source date string
:source_format: source date string format e.g '%m/%d/%Y'
:target_format: e.g '%m-%d-%Y'
:return: formatted date string
"""
date_obj = datetime.datetime.strptime(raw_date, source_format)
result = date_obj.strftime(target_format)
return result
if __name__ == "__main__":
with open("dates.txt", 'r') as f:
for line in f.read().splitlines():
print(line)
raw_date = line.split(',')[1]
date = format_to_format(raw_date, '%m/%d/%Y', '%m, %d, %Y')
print(date)

Standard modules like
csv
and datetime
are your friend!
from io import StringIO
import csv
import datetime as dt
def parse(fin):
fmt = "%B, %d, %Y," # Month, date, yyyy
sheet = csv.reader(fin)
for name, date in sheet:
date = dt.datetime.strptime(date, "%m/%d/%Y")
print(date.strftime(fmt), name)
if __name__ == "__main__":
parse(StringIO("Richard Alexander,12/21/1995"))
output:
December, 21, 1995, Richard Alexander

Here is a possible solution:
from datetime import datetime
name_and_date = "Richard Alexander,12/21/1995"
name, date_str = name_and_date.split(",")
date = datetime.strptime(date_str, "%m/%d/%Y")
formatted_date = date.strftime("%B %d, %Y")
print(f"Name: {name}\nDate: {formatted_date}")
Name: Richard Alexander
Date: December 21, 1995

Well youd have to use 2 different python functions.
One is split like so:
Dates = line.split('/')
And then use join like so :
','.join(Dates)
Of course youll need to iterate over it in a four loop beacuse you get a lot of 3 close elements as a single date, or even better after splitting each line join it to a different variable,
Hope it helped:)

You could try this:
import calendar
with open("dates.txt",'r') as data_file:
for line in data_file:
data = line.split(',')
date = data[1].split('/')
data[1] = f"{calendar.month_name[int(date[0])]}, {date[1]}, {date[2]}"
# write data to a new file here

Using standard libraries to parse and reformat the time and handle a CSV correctly:
import csv
import datetime as dt
# Never use the default encoding. It's OS-specific.
# newline='' is a documented requirement for csv.reader/writer.
with (open('input.csv', 'r', encoding='utf8', newline='') as fin,
open('output.csv', 'w', encoding='utf8', newline='') as fout):
reader = csv.reader(fin)
writer = csv.writer(fout)
for data in reader:
d = dt.datetime.strptime(data[1], '%m/%d/%Y')
data[1] = d.strftime('%B %d, %Y')
writer.writerow(data)
input.csv:
Richard Alexander,12/21/1995
John Smith,1/2/2002
Kilroy Washere,5/10/2010
output.csv:
Richard Alexander,"December 21, 1995"
John Smith,"January 02, 2002"
Kilroy Washere,"May 10, 2010"
Using pandas:
import pandas as pd
df = pd.read_csv('input.csv', encoding='utf8', header=None, parse_dates=[1])
df.to_csv('output.csv', encoding='utf8', index=False, header=None, date_format='%B %d, %Y')
(same output)
See Also: Format Codes

Related

Saving a csv with current date and time

I am running some code and I would like to save a csv file which include the current date and time in its name.
For example: I run some code now (12:24, Jan 15) and I would like to have something like
name_1224_01152021.csv
Can you tell me how to print/save this information, please?
The following code should format the name as per your requirement:
import datetime
name = f'name_{datetime.datetime.now().strftime("%H%M_%m%d%Y")}.csv'
print(name)
# prints 'name_0628_01152021.csv'
Here is the code according to your question :
from datetime import datetime
filename = datetime.now().strftime('filename_%H%M_%m%d%Y.csv')
with open(filename, "w+") as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(["row1", "row2"])
in filename you have to write your file name the output of this will be shown as
filename_0620_01152021_.csv
Something like this might be what you want :
from datetime import date
today = date.today()
# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
fname = "name_1224" + d1 + ".csv"
#fname = "name_1224" + str(d1) + ".csv"
import datetime
val_time =datetime.time.now()
now you can append name and val_time and save file with that name
You can format your date with strftime.
import datetime
filename = datetime.datetime.now().strftime('name_%H%_%d%m%Y.csv')
then you can do something like
open(filename, "w").write("blahblah")

Control Breake Logic Using Date

I am having a hard time figuring out how to read multiple lines of a text file and produce the required output. I tried the datetime format but it is not going anywhere. I would greatly appreciate any help
What is being asked is:
Write Photo Report - For this part of the project we will take a data file and use Control Break logic on it to produce a report to the screen. The control break will be on the Year the file was created. The data file will have the following format (one field per line):
Date Created (in the form DD-MM-YYYY) Filename
Number of Bytes
For example, the following input file:
25-02-2019
MyTurtle.GIF
6000
11-05-2019
Smokey.GIF
4000
I am not able read and output the date in the file. What I currently have is:
def openFile(self):
myFile = self.inputFile.getText()
fileName = open(myFile, "r")
text = fileName.readline()
x = "%4s%25s%25s\n\n" % ("File Name", "Date Created", "Number of Bytes")
date_str_format = '%Y-%m-%d'
jobs = []
for i in fileName:
d = datetime.strptime(i[0],'%m-%d-%Y')
if d in i:
date = i
x += "%4d\n" % date
You can use the built-in module re to extract all the file names, dates and bytes from your file:
import re
from datetime import datetime
with open('file.txt', 'r') as f:
t = f.read()
dates = re.findall('\d\d-\d\d-\d\d\d\d', t) # Find all the dates in the form of 00-00-0000
files = re.findall('\w+\.\w+', t) # Find all the file names in the form of text.text
byte = re.findall('(?<!-)\d\d\d\d', t) # Find all the number of bytes in the for of a four digit number without a dash behind it
print("File Name".ljust(15), "Date Created".ljust(15), "Number of Bytes".ljust(15))
for d, f, b in zip(dates, files, byte):
print(f.ljust(15), d.ljust(15), b.ljust(15))
Output:
File Name Date Created Number of Bytes
MyTurtle.GIF 25-02-2019 6000
Smokey.GIF 11-05-2019 4000
For this, you need to read all the lines in the file then process 3 lines at a time to generate a data row for the report.
Try this code:
from datetime import datetime
ss = '''
25-02-2019
MyTurtle.GIF
6000
11-05-2019
Smokey.GIF
4000
'''.strip()
with open ('myfile.txt','w') as f: f.write(ss) # write data file
#############################
def openFile(self):
fileName = open('myFile.txt', "r")
x = "{:<20}{:<25}{:<25}".format("File Name", "Date Created", "Number of Bytes")
print(x) # header row
lines = fileName.readlines() # all lines in file
for i in range(0,len(lines),3): # index every 3 lines
dt = lines[i].strip() # date
filename = lines[i+1].strip() # file name
filesize = lines[i+2].strip() # file size
d = datetime.strptime(dt,'%d-%m-%Y') # format date
x = "{:<20}{:<25}{:<25}".format(filename, str(d), str(filesize)) # data row
print(x)
openFile(None)
Output
File Name Date Created Number of Bytes
MyTurtle.GIF 2019-02-25 00:00:00 6000
Smokey.GIF 2019-05-11 00:00:00 4000

Delete row from text file if date is older than 24 hours

I have a text file called temp.txt and I want to delete all rows in it if the date is older than 24 hours from 21:45pm everyday. I've done a lot of googling and can't find the answer anywhere. The text file is in this format with no headers:
http://clipsexample1.com,clips1,clipexample123,2019-03-28 17:14:14
http://clipsexample12com,clips2,clipexample234,2019-03-27 18:56:20
Is there anyway I could remove the whole row if it is older than 24 hours (the second clip in the example)
EDIT: I have tried using this code but that's just removing todays date, how do I get it to remove today-24 hours?
save_path = 'clips/'
completeName = os.path.join(save_path, 'clips'+str(today)+'.txt')
good_dates = [str(today)]
with open('temp.txt') as oldfile, open(completeName, 'w') as newfile:
for line in oldfile:
if any(good_date in line for good_date in good_dates):
newfile.write(line)
EDIT 30/03/2019: Here is my full code to try and understand how the timestamp field is created:
#change UNIX to standard date format
def get_date(created_utc):
return dt.datetime.fromtimestamp(created_utc)
_timestamp = topics_data["created_utc"].apply(get_date)
topics_data = topics_data.assign(timestamp = _timestamp)
timestamp = _timestamp
print(timestamp)
#remove UNIX data column
topics_data.drop('created_utc', axis=1, inplace=True)
#export clips to temp.txt
topics_data.to_csv('temp.txt', header=True, index=False)
import csv
from datetime import datetime, timedelta
import os
today = datetime.today()
cutoff = datetime(year=today.year, month=today.month, day=today.day,
hour=21, minute=45)
max_time_diff = timedelta(hours=24)
input_file = 'temp.txt'
save_path = './clips'
complete_name = os.path.join(save_path, 'clips'+today.strftime('%Y-%m-%d')+'.txt')
os.makedirs(save_path, exist_ok=True) # Make sure dest directory exists.
with open(input_file, newline='') as oldfile, \
open(complete_name, 'w', newline='') as newfile:
reader = csv.reader(oldfile)
writer = csv.writer(newfile)
for line in reader:
line_date = datetime.strptime(line[3], "%Y-%m-%d %H:%M:%S")
if cutoff - line_date < max_time_diff:
writer.writerow(line)
When I print the timestamp field, this is the result i get:
01 2019-03-29 01:22:09
02 2019-03-29 02:42:21
03 2019-03-28 17:14:14
04 2019-03-29 06:06:18
Name: created_utc, dtype: datetime64[ns]
And the error I am still getting is:
ValueError: time data 'timestamp' does not match format '%Y-%m-%d %H:%M:%S'
Even though the datetime is printing in that format?
Here's how to do it using the csv module as I suggested in a comment:
import csv
from datetime import datetime, timedelta
import os
today = datetime.today()
cutoff = datetime(year=today.year, month=today.month, day=today.day,
hour=21, minute=45)
max_time_diff = timedelta(hours=24)
input_file = 'date_temp.txt'
save_path = './clips'
complete_name = os.path.join(save_path, 'clips'+today.strftime('%Y-%m-%d')+'.txt')
os.makedirs(save_path, exist_ok=True) # Make sure dest directory exists.
with open(input_file, newline='') as oldfile, \
open(complete_name, 'w', newline='') as newfile:
reader = csv.reader(oldfile)
writer = csv.writer(newfile)
next(reader) # Skip header.
for line in reader:
line_date = datetime.strptime(line[3], "%Y-%m-%d %H:%M:%S")
if cutoff - line_date < max_time_diff:
writer.writerow(line)
print('done')

How to get Python to add the current time and date automatically as the filename?

I am trying to get python to automatically add the current time and date as a filename after it finishes with running the report its designed to.
Get current date from datetime module using datetime.date.today(), use strftime() to parse it to your required format and add it to your filename string.
import datetime
filename = 'XYZ Report {0}.txt'
current_date = datetime.date.today().strftime('%d %b %Y')
filename = filename.format(current_date)
# filename = XYZ Report 19 Sep 2018.txt
with open(filename) as file_obj:
# File writing logic
Answer is:
import time
import xlwt
import csv
date_string = time.strftime("%Y%m%d %H.%M")
if saving to CSV document
data_output = []
with open('C:\\Users\\Desktop\\File Name' + date_string + '.csv', 'a+') as f:
w = csv.writer(f, delimiter=',')
for data in data_output:
w.writerow(data)
f.close()
if saving to Excel document
df.to_excel('C:\\Users\\Desktop\\File Name' + date_string + '.xlsx')
if saving to Txt document
df.to_csv('C:\\Users\\Desktop\\File Name' + date_string + '.txt')

How can I convert a string type column in a class to datetime in parse.com?

I have a column filled with dates. I imported all of the data using the csv importer. (parse.com)
I have tried reformatting the dates to match the formatting used in parse's other datetime fields. Here's the python I used to reformat the initial dates. It runs in a folder which contains my monthly logs as csv files. The date for changing is in the fifth column (ie - row[4]).
import os
import csv
import datetime
import time
from os import listdir
mypath = os.getcwd()
def find_csv_filenames( path_to_dir, suffix=".csv" ):
filenames = listdir(path_to_dir)
return [ filename for filename in filenames if filename.endswith( suffix ) ]
for each in find_csv_filenames(mypath):
with open(each,'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
next(spamreader, None) # skip the headers
for row in spamreader:
# twilio formatting "yyyy-MM-dd hh:mm:ss PDT" / or sometimes PST
date_object = datetime.datetime.strptime(row[4][:-4], "%Y-%m-%d %H:%M:%S")
# parse formatting"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"
row[4] = date_object.strftime("%Y-%m-%dT%H:%M:%S.1%MZ")
row[2] = ''
print ', '.join(row)

Categories