1

I am using Python 3.6 and displaying the contents of a CSV file through the following code:

#!/usr/bin/env python3

import csv
import sys
import json

def main():
    try:
        with open('file.csv') as csvfile:
            readCSV = csv.DictReader(csvfile, delimiter = ',')
            for row in readCSV:
                print(json.dumps(row))
    except Exception as msg:
        print(msg)
        sys.exit(1)

if __name__ == '__main__':
    main()

This code works perfectly fine. However, what I noticed was when this code is run in linux, the output is printed in an incorrect order from the CSV file and when run in Windows, it outputs it as expected.

Am I missing something to run this correctly in linux?

EDIT:

This is the correct output from Windows:

{"First Name": "Bob", "Last Name": "Clay", "Phone": "1234567890", "ID":     "4355", "Salary": "899332", "Year": "2002"}
{"First Name": "Mo", "Last Name": "Jo", "Phone": "2848193919", "ID": "1223", "Salary": "872234", "Year": "1998"}
{"First Name": "Steve", "Last Name": "Mann", "Phone": "4732874757", "ID": "1949", "Salary": "75699", "Year": "2015"}
{"First Name": "John", "Last Name": "Smith", "Phone": "1287823408", "ID": "4859", "Salary": "456909", "Year": "1890"}

This is the incorrect output from Linux:

{"Last Name": "Clay", "Phone": "1234567890", "Year": "2002", "ID": "4355", "Salary": "899332", "First Name": "Bob"}
{"Last Name": "Jo", "Phone": "2848193919", "Year": "1998", "ID": "1223", "Salary": "872234", "First Name": "Mo"}
{"Last Name": "Mann", "Phone": "4732874757", "Year": "2015", "ID": "1949", "Salary": "75699", "First Name": "Steve"}
{"Last Name": "Smith", "Phone": "1287823408", "Year": "1890", "ID": "4859", "Salary": "456909", "First Name": "John"}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
comp32
  • 201
  • 1
  • 5
  • 13

0 Answers0