-1

I am looking to see if I can find a more efficient way to write this. The problem with csv reader is that when you write the output to standard out it tosses additional single quotes around it. Changing the quotes to none didn't help because it didn't retain formatting.

This code works but I have a feeling that I could do it more efficiently. I am really new to python and programming.

import csv
import sys

def printString(x):
    print x[0] + ",", x[1] + ",", x[2] + ",", x[3] + ","

with open(sys.argv[1],"rb") as inputFile:
    csvInput = csv.reader(inputFile, delimiter=',')
    header = next(csvInput)
    sort = sorted(csvInput, key=lambda x:float(x[3]))

printString(header)
for i in sort:
   printString(i)

4 Answers4

1

Use this way:

def printString(x):
    for field in x:
        print "{0},".format(field)
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
  • 2
    While this code snippet may solve the problem, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – MattDMo Apr 04 '16 at 20:37
  • to clarify a bit. I would like to be able to send the output to another file if needed and have a proper csv file. example: ./csvsort.py csvFile.csv > newCsvFile.csv – John Smith Apr 04 '16 at 21:32
1

Read Understanding repr( ) function in Python, it applies to your "csv output": csv.reader produces lists of strings as rows. When you pass a list as is to print statement, it prints its repr representation:

>>> print ["f1", "f2", "f3", "f4"]
['f1', 'f2', 'f3', 'f4']

Print as a function would come in handy in this case:

from __future__ import print_function

for row in sort:
    print(*row, sep=", ")

Using the previous example:

>>> print(*["f1", "f2", "f3", "f4"], sep=", ")
f1, f2, f3, f4

The asterisk in this context unpacks the list as positional arguments to print.

Community
  • 1
  • 1
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
0

I'd look at the string.join function - you can use that by providing the string you want to use as a separator (in this case, the comma), calling .join on it, and then passing in the array of values (in this case, the values from your csvreader.

>>> x = ['1.0', '2.0', '3.0']
>>> ','.join(x)
1.0,2.0,3.0

warning - join requires the values in the array to solely be strings (so if you wanted to do any conversion to ints/floats for processing then you might want to look at the other options provided here

bbm
  • 130
  • 1
  • 1
  • 7
0

Thanks for your help, here is what I decided to go with.

import csv
import sys

try:
    with open(sys.argv[1],"rb") as inputFile:
        csvInput = csv.reader(inputFile, delimiter=',')
        header = next(csvInput)
        sort = sorted(csvInput, key=lambda x:float(x[3]))
    print(','.join(header))
    for i in sort:
        print(','.join(i))

except:
    print("Please input a csv file  to sort, example: './dataSort.py csvFile.csv'" + \
                " the 4th column of the csv file needs to be a float or int variable.")