Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7016749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:49:32+00:00 2026-05-27T22:49:32+00:00

I’ve been sitting for some time with a task to import serverlogs from a

  • 0

I’ve been sitting for some time with a task to import serverlogs from a .txt file, write it out to a python dictionary and write it further out to a csv file to make it readable in a database. When I open it in fx. excel, I want it to be listed with headers.

EDIT: I need this to be generic in the way that I can load a log file of any length and still get everything. Edit done.

I’m stuck at the part of writing the dictionary down to the csv file. My following script looks like the following, and the seperated, out commented files are tries i’ve used to make it write it out.

    lineArray = []
    timeArray = []
    typeArray = []
    whoArray = []
    idArray = []
    msgArray = []

    infile = open('C:\SomeArea\data.txt', 'r')
    dictionary = {'time','type','who','id','msg'}
    for line in infile:
        timeInString = line[:24]
        timeInStringStripped = timeInString.strip()
        timeArray.append(timeInStringStripped)
        allTimes = ', '.join(timeArray)

#The last 4 fields was removed here to take less space

    infile.close()
    dictionary = {'time': allTimes, 'type': allTypes, 'who': allWhos, 'id': allIds, 'msg': allMsgs}

Half attempts to write it to the csv

#<><><><><><><><><><><><><><><><><><><><><><><>#
#str = 'lement1, lement2'
#arr = ['lement1, lement2']

#import csv
#f = open('sometest2.csv', 'wb')
#wtr = csv.DictWriter(f, dictionary.viewkeys())
#for element in dictionary.viewitems():
#    print element
#    for e in element:
#        print e
#wtr.writerow(0)
#f.close()
#wtr.writerows(allTimes)
#wtr.writerows(allTypes)
#wtr.writerows(allWhos)
#wtr.writerows(allIds)
#wtr.writerows(allMsgs)
#print dictionary

#<><><><><><><><><><><><><><><><><><><><><><><>#

import csv
f = open('sometest2.csv', 'wb')
wtr = csv.DictWriter(f, dictionary.viewkeys())
for row in dictionary: f.write("%s%s" %(delimiter.join([row[name] for name in fieldnames]),lineterminator))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:49:33+00:00Added an answer on May 27, 2026 at 10:49 pm

    The problem is that the code attempts to write a dictionary of lists and csv.DictWriter expects a list of dictionaries.

    The right way to parse your data to work with the csv module would be to store each row as a dictionary. Hence instead of having something like this:

    dictionary = {'time': allTimes,
                  'type': allTypes,
                  'who': allWhos,
                  'id': allIds,
                  'msg': allMsgs}
    

    You should have something like this:

    data = [{'time': time_row_1,
             'type': type_row_1,
             'who': who_row_1,
             'id': id_row_1,
             'msg': msg_row_1},
    
            ...
    
            {'time': time_row_n,
             'type': type_row_n,
             'who': who_row_n,
             'id': id_row_n,
             'msg': msg_row_n},
           ]
    

    Hence, the code to read from a file should be like:

    data = []
    for line in infile:
        row = {}
        row['time'] = ...
        row['type'] = ...
        ...
        ...
        data.append(row)
    

    and the code to write to a file would be:

    wtr = csv.DictWriter(open(filename, 'w'), keys)
    wtr.writerows(data)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.