0

This script is meant to parse and sort a list from a csv file and save to a newly created csv file, including headers.

I triyng to include the write function to save the output of this parser to a new csv file with the following. This code create a csv but record the headers only and in one column.

Here's the input:

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},

Here's the output I'd like to get saved to 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,,,,,

The code:

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)
9
  • Your indentation is wrong. after def stmt you need to add indent. docs.python.org/2/tutorial/controlflow.html#defining-functions Commented Jan 7, 2015 at 8:40
  • Thanks, I believe this is properly indented now. Commented Jan 8, 2015 at 3:30
  • Sample input? Desired output example? Commented Jan 8, 2015 at 3:35
  • 1
    This code doesn't run as well. Fix the errors ("wd" isn't a valid string for open, indentation is still wrong after if line1 in path and missing :). Commented Jan 8, 2015 at 3:48
  • added input as well as output I'd to get. I think I fixed errors you mentioned. still don't run though Commented Jan 8, 2015 at 4:54

2 Answers 2

0

You have several problems in your code,

First take this to top of your file

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

Use DictWriter to write 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)

Use you mapping

        csv_writer(mappedLines, path)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! great help. However I still need to write the first 8 column to my newly created csv. not sure how to do that
0

csv_writer references a symbol line -- this is not an argument to the function. How are you expecting that to be provided?

1 Comment

How are you expecting that to be provided? Sorry, I'm not sure to understand what you mean. I have remore the underscore in the function's name

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.