简体   繁体   中英

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.

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
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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM