0

I have a CSV file with many rows and column. I wanted to create one folder for each row and within that folder have two files:

  1. First 5 columns
  2. Next columns

I have been able to create two cases

  1. I have been able to create two folders 'a','b' 'a' having all the files of first five columns. and vice-versa.
  2. I have been able to create a single folder having the name of the time stamp. and all the files in it.

    import pandas as pd import os from datetime import datetime from datetime import timestamp as ts data = pd.read_csv('test.csv')

    if '10' not in os.listdir(os.getcwd()): os.mkdir(timestamp) if '50' not in os.listdir(os.getcwd()): os.mkdir('50')

    def splitter(data, split = 3): timestamp = datetime.timestamp(datetime.now()) for i in data.itertuples():

        data_1 = pd.Series(i[1:split+1])
        data_2 = pd.Series(i[split+1:])
        data_1.drop
        #print(data_1)
        data_1.to_csv(r'10.txt'.format(i[0]+1),mode = 'w',index = False)
        data_2.to_csv(r'50.txt'.format(i[0]+1),mode = 'w',index = False)
    

    splitter(data)

I want to have name of different folders having time as their name to stop redundancy

4
  • 1
    What if you have 12 columns in 2 rows? How would it be arranged in that case? Commented Jul 5, 2019 at 8:30
  • it can be split in any manner 4, 8 Commented Jul 5, 2019 at 8:44
  • it will have 2 folders Commented Jul 5, 2019 at 8:45
  • one for each row and each folder will have two files having first 4 cloumns in one file and 8 coloumns in other file Commented Jul 5, 2019 at 10:07

1 Answer 1

0

As per your comment, one folder per row, this might do.

import csv
import os
with open('test.csv','r')as f:
    data = csv.reader(f)


    def splitter(data, split = 3):
        rowNumber = 0
        for row in data:
            if rowNumber == 0:
                rowNumber += 1
                continue
            os.mkdir("folder-"+str(rowNumber))

            # Split it and create individual files
            with open("folder-"+str(rowNumber)+"/"+"10.csv","w") as tfile:
                tfile.write(",".join(row[0:5]))

            with open("folder-"+str(rowNumber)+"/"+"50.csv","w") as tfile:
                tfile.write(",".join(row[5:]))
            rowNumber += 1

    splitter(data)

I removed the Pandas library as it didn't seem required. Rather used the CSV library. The for loop takes each row creates a folder for it and then the two required files.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.