1

really appreciate your help.

I have around 200 csv files with same header. eg of headers are x , y, z, time, id, type

I would like to sort out time colums of all csv files and save them again.

This is so far I have tried. But it doesn't work.

Could you please help me ??

Thank you


import csv
import operator
import glob
import pandas as pd

data = dict() # filename : lists

path="./*.csv"
files=glob.glob(path)

for filename in files:
    # process each file
    with open(filename, 'r') as f:
        # read file to a list of lists
        lists = [row for row in csv.reader(f, delimiter=',')]
        # sort and save into a dict
        sorted_df = lists.sort_values(by=["time"], ascending=True)
        sorted_df.to_csv('%.csv', index=False)
5
  • If you want them sorted by time, why would you sort them by "id"? Commented Mar 11, 2021 at 4:20
  • So, this is not your real code? Because this will create a file called %i.csv and overwrite it, over and over and over. Commented Mar 11, 2021 at 4:31
  • yes, not real one. I added sorted_df lines to save files. Commented Mar 11, 2021 at 4:40
  • So, what is your problem? Commented Mar 11, 2021 at 5:00
  • 1
    It wasn't working before. I solved it. Thanks a lot for your time. Commented Mar 11, 2021 at 5:10

1 Answer 1

1

I don't have much knowledge about the csv module but you're using pandas and it supports reading csv files with pd.read_csv, why not utilize that..

for filename in files:
  df = pd.read_csv(filename)
  df.sort_values('time', inplace=True)
  df.to_csv(filename, index=False)

This would overwrite all the files with same data sorted by time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.