Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Combine a folder of text files into a CSV with each content in a cell

I have a folder containing several thousand .txt files. I'd like to combine them in a big .csv according to the following model:

enter image description here

I found a R script supposed to do the job (https://gist.github.com/benmarwick/9265414), but it displays this error.

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : duplicate 'row.names' are not allowed 

I don't understand what's my mistake.

No matter, I'm pretty sure there's a way to do that without R. If you know a very elegant and simple one, it would be appreciated (and useful for a lot of guys like me)

PRECISION : the text files are in french, so not ASCII. Here is a sample : https://www.dropbox.com/s/rj4df94hqisod5z/Texts.zip?dl=0

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The following python script works for me (where path_of_directory is replace by the path of the directory your files are in and output_file.csv is the path of the file you want to create/overwrite):

#! /usr/bin/python

import os
import csv

dirpath = 'path_of_directory'
output = 'output_file.csv'
with open(output, 'w') as outfile:
    csvout = csv.writer(outfile)
    csvout.writerow(['FileName', 'Content'])

    files = os.listdir(dirpath)

    for filename in files:
        with open(dirpath + '/' + filename) as afile:
            csvout.writerow([filename, afile.read()])
            afile.close()

    outfile.close()

Note that this assumes everything in the directory is a file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...