-
-
Save jorgemanrubia/a2abf7d79a4a4ba31ba833631d1ee292 to your computer and use it in GitHub Desktop.
chatGPT prompt 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from datetime import datetime | |
# Open the input CSV file | |
with open('input.csv', 'r') as input_file: | |
# Create a csv reader object | |
reader = csv.reader(input_file) | |
# Skip the header row | |
next(reader) | |
# Initialize a dictionary to store the data by date | |
data = {} | |
# Iterate over the rows in the input file | |
for row in reader: | |
# Extract the timestamp and convert it to a datetime object | |
timestamp = datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S') | |
# Calculate the milliseconds since midnight | |
milliseconds = int((timestamp - timestamp.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() * 1000) | |
# Extract the date from the timestamp | |
date = timestamp.date() | |
# If the date is not already in the dictionary, initialize a list for that date | |
if date not in data: | |
data[date] = [] | |
# Append the row data to the list for that date | |
data[date].append([milliseconds] + row[1:]) | |
# Iterate over the data by date | |
for date, rows in data.items(): | |
# Open a new CSV file with the date as the file name | |
with open(f"{date}.csv", 'w') as output_file: | |
# Create a csv writer object | |
writer = csv.writer(output_file) | |
# Write the rows to the output file | |
writer.writerows(rows) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
could you rewrite that without using pandas, just use the standard CSV library |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment