简体   繁体   中英

Python: how to sort existing csv files using in python

I have about 200 csv files with the same number of columns: A,B,C,D,E. I want to sort all of them by column B and then column A. Can do this in Python?

I created a sort program for csv files that outputs a new sorted csv file with two keys. In order to sort, first sort by the secondary key and then by the primary key

To sort multiple files, loop over all the input files creating the basic statistics array. Afterward sort the result.

I only had one input file so I did not have to do that. Here is what I did for one file. You would change where I have infile to be the result of the input loop.

ifile = open('file.csv', 'rb')
infile = csv.DictReader(ifile)
infields = infile.fieldnames
try:
  # This assumes that the first row is data
  sortedlist = sorted(infile, key = lambda d: float(d['statistic2'], reverse =dir) # dir is True or False
except ValueError:
  # Go back and skip header 
  ifile.seek(0)
  ifile.next()
  sortedlist = sorted(infile, key = lambda d: float(d['statistic2'], reverse =dir) # dir is True or False
# Now do the primary key.
  sortedlist.sort(key = lambda d: float(d['statistic1'], reverse =dir) # dir is True or False

ifile.close()

Now open the output file using csv.DictWriter, write the header and output the data from sortedlist.

csv is a standard text file (not an Excel file). Python can certainly process these files. There is a library called csv which is designed for just this type of work: http://docs.python.org/2/library/csv.html

Assuming the file sizes are manageable, you should be able to simply load them all into memory and then sort.

What have you tried so far?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM