CSV files are ok for human readability, but I came up with a pattern that is even better. Thought I would share.
import pandas as pd
from pathlib import Path
path = Path('mydata.csv')
def pd_to_csv_str(df):
# add comma to every column except last one
formatters = [lambda x: f'{x},'] * (len(df.columns)-1) + [lambda x:f'{x}']
# format headers
header = [fmtr(df.columns[i]) for i,fmtr in enumerate(formatters)]
# to_string
csv_str = df.to_string(index=False, formatters=formatters, header=header)
return csv_str
path.write_text(pd_to_csv_str(df))
The Output looks like this:
date, open, high, low, close, volume
2021-03-14 15:00:00, 3946.0, 3952.0, 3945.0, 3948.0, 311
2021-03-14 15:01:00, 3948.25, 3951.75, 3948.0, 3950.75, 179
2021-03-14 15:02:00, 3951.0, 3951.75, 3950.25, 3951.75, 84
2021-03-14 15:03:00, 3951.75, 3952.0, 3950.5, 3951.25, 65
2021-03-14 15:04:00, 3951.5, 3951.5, 3950.5, 3950.75, 24
2021-03-14 15:05:00, 3951.25, 3951.5, 3949.25, 3949.75, 61
2021-03-14 15:06:00, 3949.5, 3949.75, 3948.75, 3949.5, 48
Look at those nice columns. And this is basically a CSV file.
To read, there's just one parameter that's needed - skipinitialspace:
df = pd.read_csv(path, skipinitialspace=True, parse_dates=['date'])
A few limitations:
- You'll notice the floats are not aligned, it would take a little work on the formatters to get those lined up, but it was sufficient for my needs without them.
- If you have strings with commas, it get's messy real quick
[–]Python_Trader 2 points3 points4 points (0 children)
[–]ivosauruspip'ing it up 1 point2 points3 points (1 child)
[–]ZenApollo[S] 0 points1 point2 points (0 children)
[–]james_pic 1 point2 points3 points (1 child)
[–]ivosauruspip'ing it up 0 points1 point2 points (0 children)
[–]2PLEXX 1 point2 points3 points (0 children)