简体   繁体   English

在python中为电子表格重新格式化CSV文件

[英]Reformat CSV file in python for spreadsheet

I have a text file text.csv with dates arranged as such. 我有一个文本文件text.csv ,其日期安排了。

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: 我想重新格式化或将文件写入2列,例如:

+---------------+-----+
| 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,但可以遍历数据:

<?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? 您能否提供一个可以帮助我入门的python示例? It needs to run as a macro in Libre office calc. 它需要在Libre office calc中作为宏运行。

As I said in a comment, your input file isn't a CSV file. 正如我在评论中所说,您的输入文件不是CSV文件。 You could use the following to do the formatting you want and produce a valid CSV file. 您可以使用以下内容进行所需的格式化,并生成有效的CSV文件。 Many spreadsheet programs can read CSV files that use either a comma or tab ('\\t') character as a delimiter. 许多电子表格程序可以读取使用逗号或制表符('\\ t')字符作为分隔符的CSV文件。

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 . 现在您必须读取文件并写行来代替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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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