Python script for reformatting a text file into a csv using Python
<p>I have been asked to read in a text file containing this:</p>

<pre class="lang-none prettyprint-override"><code>1.

Wicked Stepmother (1989) as Miranda

A couple comes home from vacation to find that their grandfather has â¦

2.

Directed By William Wyler (1988) as Herself

During the Golden Age of Hollywood, William Wyler was one of the â¦

3.

Whales of August, The (1987) as Libby Strong

Drama revolving around five unusual elderly characters, two of whom â¦

4.

As Summers Die (1986) as Hannah Loftin

Set in a sleepy Southern Louisiana town in 1959, a lawyer, searches â¦
</code></pre>

<p>and to create a .csv output file that looks like this:</p>

<pre class="lang-none prettyprint-override"><code>1,Wicked Stepmother ,1989, as Miranda,A couple comes home from vacation â¦
2,Directed By William Wyler ,1988, as Herself,During the Golden Age of â¦
3,"Whales of August, The ",1987, as Libby Strong,Drama revolving around fiveâ¦
</code></pre>

<p>I know that if I can slice the lines apart, then I can add them back together again with commas in between them and then write those strings into my output file. My problem is with the format. For the numbers I would only want:</p>

<pre><code>line1=stringname[0]+','
line2= stringname[:stringname.find('(')-1]+','+stringname[stringname.find('(')+1:stringname.find(')')-1]+','+stringname[stringname.find(')')+1:]
</code></pre>

<p>no change to line3 then write into the file</p>

<pre><code>result=line1+line2+line3
</code></pre>

<p>The problem is I have no idea which line I am parsing at any given time. I was thinking maybe something in a for loop which makes sure I parse the code in groups of 3 lines at a time, but I am not sure how to manage the file handling at the same time. I am also not sure how to prevent the loop from going over the end of the program.</p>

<p>This could be done easily using a regular expression but I am guessing you do not wish to use that. </p>

<p>Instead the problem can be solved by reading the file in a line at a time and deciding if the line starts with a number followed by a <code>.</code>. If it does, start you start building up a list of lines until you find the next number.</p>

<p>Using Python's <code>int()</code> function will attempt to convert a string into a number. The <code>find('.')</code> function attempts to locate the end of the number.</p>

<p>If the returned string is not a number, it causes a <code>ValueError</code> exception to be raised. In this case, add the line to the list of lines.</p>

<p>If there was a number, first write any existing entry to the <code>csv</code> file, and then start a new entry.</p>

<p>At the end, there will not be a final number line to trigger the next write, so add another call to write the final row to the csv.</p>

<p>For example:</p>

<pre><code>import csv        

with open('text.txt') as f_input, open('output.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    entry = []

    for line in f_input:
        line = line.strip()    # Remove the trailing newline

        if len(line):          # Does the line containing anything?
            try:
                number = int(line[:line.find('.')])

                if len(entry):
                    csv_output.writerow(entry)
                entry = [number]
            except ValueError:
                entry.append(line)

    csv_output.writerow(entry)
</code></pre>

<p>Python's <code>csv</code> library is used to take a list and automatically add the necessary commas between entries when writing to the csv output file. If an entry contains a comma, it will automatically add quotes.</p>

python string csv file-handling