Search code examples
pythonlibreoffice-calc

Reformat CSV file in python for spreadsheet


I have a text file text.csv with dates arranged as such.

name1
2010-01-02 (i)
2010-05-07 (i)
2010-06-12 (i)
name2
2010-01-02 (i)
2010-05-07 (i)
2010-06-12 (i)
name3
2011-01-05 (i)
2011-05-05 (i)
2011-06-14 (i)

I'd like to reformat or write the file into 2 columns like:

+---------------+-----+
| 2010-01-02 (i)|name1|
| 2010-05-07 (i)|name1|
| 2010-06-12 (i)|name1|  
| 2010-01-02 (i)|name2|
| 2010-05-07 (i)|name2|
| 2010-06-12 (i)|name2|
| 2011-01-05 (i)|name3|
| 2011-05-05 (i)|name3|
| 2011-06-14 (i)|name3|
+---------------+-----+

The logic would be something like:

if line doesn't contain "(i)", name=value
else
write date=value, name to file

I'd rather not use PHP, but I could loop through the data:

<?php
$file = file($path);
foreach($file as $value)
{
  if ( strpos($value, "(i)" ) !== false)
    $name = $value;

    $fp = fopen('data.csv', 'w');
    fputcsv($fp, $line);
    fclose($fp);
}

Can you provide a python example that could get me started? It needs to run as a macro in Libre office calc.


Solution

  • As I said in a comment, your input file isn't a CSV file. You could use the following to do the formatting you want and produce a valid CSV file. Many spreadsheet programs can read CSV files that use either a comma or tab ('\t') character as a delimiter.

    import csv
    DELIMITER = ','
    
    with open('data.csv', 'wb') as csvfile:
        writer = csv.writer(csvfile, delimiter=DELIMITER)
        row = [None, None]
        with open('input_data.txt', 'rt') as textfile:
            for line in (line.strip() for line in textfile):
                if line.endswith('(i)'):
                    row[0] = line
                    writer.writerow(row)
                else:
                    row[1] = line