0

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.

1
  • Although there's no standard, neither the input nor the output file are CSV format. Commented Jul 5, 2014 at 16:48

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1
data = '''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)'''

name = None
for line in data.splitlines():
    if '(i)' in line:
        print line, name
    else:
        name = line

result:

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

Now you have to read file and write lines in place of print.

Comments

1

Another, rather simple approach:

lines = []

with open('original.txt') as f:
    for line in f:
       if line.startswith('name'):
           key = line.rstrip()
       else:
           lines.append('{} {}'.format(line.rstrip(), key))

with open('output.txt', 'w') as f:
    f.writelines(lines)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.