在python和csv模块中解析和保存csv文件

时间:2015-01-07 08:21:05

标签: python parsing csv

此脚本用于解析csv文件中的列表并对其进行排序,并保存到新创建的csv文件中,包括标题。

我试图包含write函数以将此解析器的输出保存到具有以下内容的新csv文件中。此代码创建一个csv,但仅在一列中记录标题。

这是输入:

Timestamp,Session Index,Event,Description,Version,Platform,Device,User ID,Params,
"Dec 27, 2014 05:26 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,{},
"Dec 27, 2014 05:24 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,{},
"Dec 27, 2014 05:23 AM",1,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807;  tabName : Home},
"Dec 27, 2014 05:23 AM",2,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807;  tabName : Home},
"Dec 27, 2014 05:23 AM",3,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807;  tabName : QuickAndEasy},

这是我希望保存到csv的输出:

Timestamp,Session Index,Event,Description,Version,Platform,Device,User ID,TabName,RecipeID,Type,SearchWord,IsFromLabel,
"Dec 27, 2014 05:26 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,,,,,,
"Dec 27, 2014 05:24 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,,,,,,
"Dec 27, 2014 05:23 AM",1,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,Home,,,,,
"Dec 27, 2014 05:23 AM",2,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,Home,,,,,
"Dec 27, 2014 05:23 AM",3,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,QuickAndEasy,,,,,

代码:

import csv


def printfields(keys, linesets):
    output_line = ""
    for key in keys:
        if key in linesets:
            output_line += linesets[key] + ","
        else:
            output_line += ","
    print output_line


def csvwriter(reader, path):
    """
    write reader to a csv file path
    """
    with open(path, "w") as csv_file:
        writer = csv.writer(csv_file, delimiter=",")
        for line1 in line:
            if line1 in path:
                writer.writerow(line1)

if __name__ == "__main__":
    fields = [
        "UserID", "tabName", "RecipeID", "type", "searchWord", "isFromLabel", "targetUID"
    ]
    mappedLines = {}
    with open('test.csv', 'r') as f:
        reader = csv.DictReader(f)
        for line in reader:
            fieldPairs = [
                p for p in
                line['Params'].strip().strip('}').strip('{').strip().split(';')
                if p
            ]
            lineDict = {
                pair.split()[0].strip(): pair.split(':')[1].strip()
                for pair in fieldPairs
            }
            mappedLines[reader.line_num] = lineDict
        path = "output.csv"
        csvwriter(reader, path)

    for key in sorted(mappedLines.keys()):
        linesets = mappedLines[key]
        printfields(fields, linesets)

2 个答案:

答案 0 :(得分:0)

csv_writer引用符号line - 这不是函数的参数。您对此有何看法?

答案 1 :(得分:0)

您的代码中存在多个问题,

首先将其置于文件顶部

fields = [
    "Timestamp","Session Index","Event","Description","Version","Platform","Device","User ID","Params",""
]

使用DictWriter撰写dict

def csv_writer(lines, path):
    """
    write reader to a csv file path
    """
    with open(path, "w") as csv_file:
        writer = csv.DictWriter(csv_file, fields)
        writer.writeheader()
        # Iterate over dict
        for line1, val in lines.iteritems():
            writer.writerow(val)

使用您的映射

        csv_writer(mappedLines, path)