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 7783731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:55:27+00:00 2026-06-01T19:55:27+00:00

I an attempting to use the python csv module to write a dictionary to

  • 0

I an attempting to use the python csv module to write a dictionary to a csv file.

My dictionary looks something like this:

{'PCIP': '192.168.1.4', 'DutIP': '192.168.1.6', 'timestamp': '20120410100340', 'start_time': '0.0', 
 'Transfer bytes': '59457090', 'Port': '5763', 'Lost Datagrams': '10575', 'Percent Lost Datagrams': 
 '20.727', 'PER': 20.727, 'Bandwidth bits': '51684334', 'Throughput': 51.684334, 'end_time': '9.2', 
 'Unknown': '41710', 'Jitter': '0.017', 'ID': '3', 'Total Datagrams': '51020'}

I get the error sequence expected when I attempt to write it.

Is there any way to do so?

def save_result_to_csv_file(self, file, data, header="", access_mode='ab'):
    """
    :param file: local file_name for csv-format logfile
    :type file: str
    :param data: a list or other datatype supported by csv.writerow() 
    :type data: list
    :param header: list containing row names
    :type header: list
    :param access_mode: 'a' append, 'w' write, 'ab' binary append, 'wb' binary write, 'r' read
    :type access_mode: str
    """
    #if file does not exist, start by writing the header row.

    log_file_obj = open(file, 'ab') 
    log_file_writer = csv.writer(log_file_obj)
    if not os.path.isfile(file):
        log_file_writer.writerow(header)
    log_file_writer.writerow(data)
    log_file_obj.flush()
    log_file_obj.close()

     # Labels (Compare to test_case_log_row definition, which matches row_labels)
    interval_data_labels = ["timestamp", "DutIP", "Port", "PCIP", "Unknown", "ID", "start_time", "end_time", "Transfer bytes", "Bandwidth bits"]
    summary_data_labels = interval_data_labels + ["Jitter", "Lost Datagrams", "Total Datagrams", "Percent Lost Datagrams"]
    timestamp_label = ["timestamp"]
    device_info_labels = [ "Device", "Device OS", "Device Bundle"]
    test_results_labels = ["Throughput (MBits/s)", "PER"]
    test_parameters_labels = ["pD.protocol", "pD.test_direction", "pD.execution_time", "pD.send_rate", "pD.packet_size", "pD.transfer_amount"]
    test_data_raw_labels = summary_data_labels
    row_labels = timestamp_label + device_info_labels + test_parameters_labels + test_results_labels + test_data_raw_labels
  • 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-06-01T19:55:29+00:00Added an answer on June 1, 2026 at 7:55 pm

    This snippet works ok in 2.7

    import csv
    
    my_dict =  {'PCIP': '192.168.1.4', 'DutIP': '192.168.1.6', 'timestamp': '20120410100340', 'start_time': '0.0', 
    'Transfer bytes': '59457090', 'Port': '5763', 'Lost Datagrams': '10575', 'Percent Lost Datagrams': 
    '20.727', 'PER': 20.727, 'Bandwidth bits': '51684334', 'Throughput': 51.684334, 'end_time': '9.2', 
    'Unknown': '41710', 'Jitter': '0.017', 'ID': '3', 'Total Datagrams': '51020'}
    
    writer = csv.writer(open("some.csv", "wb"))
    
    writer.writerows(my_dict.viewitems())
    

    If it is necessary to have keys in headers and columns to be the values one should use

    writer.writerow(list(my_dict.viewkeys()))
    writer.writerow(list(my_dict.viewvalues()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use Python's tarfile module to extract a tar.gz archive. I'd like
I'm attempting to use the python subprocess module to log in to a secure
I am attempting to use a parametrized LIKE query with Python's Sqlite library as
I'm attempting to use Paramiko (Python SSH library) to read a remote file, and
I'm attempting to use the python logging module to do complex things. I'll leave
I'm attempting to use savepoints with the sqlite3 module built into python 2.6. Every
I'm attempting to use Python's logging module to send emails containing logs. The problem
Im attempting to use a C++ extension for Python called PySndObj. and getting an
In Python 3.0.1, I am attempting to use the Counter part of the collections
Attempting to use the data series from this example no longer passes the JSONLint

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.