Wednesday, December 31, 2014

Re: Write Multiple Text Files into a Single CSV File

I'm not too sure about the format of the content, but maybe this to create the file?

import csv

files = ['f1.txt', 'f2.txt', 'f3.txt']

with open('output.csv', 'wb') as f:
    writer = csv.writer(f)
    for input_file_name in files:
        with open(input_file_name, 'r') as input_file:
            lines = input_file.readlines()
            title = lines[0].strip()
            content = ' '.join(line.strip() for line in lines[1:])
            writer.writerow([title, content])


On Wed, Dec 31, 2014 at 2:54 AM, Martin Mirero <mmirero@gmail.com> wrote:
Hi folks,

I'm just cutting my teeth on Python/Django and need some assistance on something I've been grappling with for a few days:
  • I have a bunch of text files on disk that all have the same basic format: first line is the title and the rest is the body
  • I want to create one CSV file with the first column being populated from the first line of each text file (title) and the second column being populated from the rest of the text file contents (body)
  • Once I have the CSV file, I'm good to import that into my django model
Any help would be much appreciated.

Cheers.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/07a9375c-4b05-42cc-a46e-0337ce96d6e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei17oJMCcDTdyJf-6y1wsJJO4Or2%3DgVMWYFMns45FRNtzA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment