Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

python - How to combine files in folder into one file and cut it by indexes given?

I need to combine data files into 1 file using indexes in files because the necessary data is between them. And save the data as NumPy array. Indexes are "Data_start" and "Data_end". Here is the code I'm working on:

import os
import numpy as np

f = open('combined.csv', 'w')                   # create file
filenames = os.listdir('data')                  # data files list in folder "data"
with f as combined:                             # open file 'combined.csv'
    os.chdir('data')                            # change folder to data folder
    for filename in filenames:                  # for each name in file
        with open(filename) as infile:          # read filename in file as 'infile'
          # here is the code to cut the file between [datastart:dataend] indexes ?
            combined.write(infile.read())       # write in 'combined' what you read in file
        combined.write('
');                   # put 'enter' after

#here is the code to save it as NumPy array?

in my case, it saves the file as one long line or as many long lines, but not as a normal CSV. Thanks in advance!

question from:https://stackoverflow.com/questions/65829938/how-to-combine-files-in-folder-into-one-file-and-cut-it-by-indexes-given

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...