Wednesday 15 April 2015

python - How to sort a csv file column by newest time -


I am trying to read in a CSV file which results in the autorunsc command line (i.e., autorunsc -a -m - C & gt; mycsv.csv) and according to the time it meets the latest date / time at the top, like in Excel

I have yet found out that it is a How to read the CSV file without encoding error as UTF 16, it will be re-encoded as UTF 8 Is changed.

  import sys import csv import operator import codec class recoder (object): def __init __ (self, section, decoder, encoder, eol = '\ r \ n'): self._stream = Section self._decoder = decoder if isinstance (decoder, codecs.IncrementalDecoder) and codecs.getincrementaldecoder (decoder) () self._encoder = encoder if isinstance (encoder, codecs.IncrementalEncoder) and codecs.getincrementalencoder (encoder) (self._buf) = '' Self._eol = EOL self._reachedEof = wrong def (self, size = none): r = self.stream.read (size) raw = self._decoder.code (r, size is none) returns self ._ncyder.exo (Raw) def __iter __ (self): self def __next __ (self) return: if self._reachedEof: if EOL == self._eol line, EOL, rest = self._buf.partition (self._eol): self. _buf = rest is StopIteration (), while this is true return self._encoder.encode (line + EOL) raw = sel f._stream.read (1024) if raw == '': self._decoder.decode (b ' , This is true) self._reachedEof = true return self._encoder.encode (self._buf) self._buf + = self. _decoder.decode Next = __next__def pass (self): return self._stream.close () author = open ('mycsv1.csv,' West Bengal) f = open ('mycsv.csv', 'rb' ): SR = Richer (f, 'utf-16', 'utf-8') s = sorted (csv.reader (sr), key = operator.itemgetter (0), reverse = true)) s To: Print & gt; & Gt; The author, line [0], ",", line [1], "," line [2]   

The problem looks like this month's Say I had entries from 1 to 6 months for 1, 6, 2010, 2011, 2012.

This will sort by month only and do not include time or date so that I can get the latest dates. Instead I get 1/1/2010, 1/1/2011, 1/1/2012, 2/1/2010, 2/1/2011, 2/1/2012

If I Sorting in Excel, it will give me the date / time first, in which case if this is the month of June (6/1/2012, 5/1/2012, 4/1/2012, etc.) using Python How can I achieve this?

UPDATE

I am working with sample data that is formatted in utf8 after autorunsc. The data in CSV should be as follows:

  time, entry location, entry, enabled, category, description, publisher, launch string 6/23/2011 14:23, HKLM \ System \ CurrentControlSet \ Services , JavaQuickStarterService, Enabled, Service, Prefetches JRE Files for Fast Startup, Oracle Corporation, C: \ Program Files \ Java, C: \ Program Files \ Java \ JRE \ 2006/05/25 01:14 blah ,,,, ,,,,, 4/4/2003 22:10 ,,,,,,, 17/2006 11:11 ,,,,,,,, 0 ,,,,,,,,, (Date in some entries Do not assume and are empty or empty. 6/10/2013 9:30 ,,,,,,,,,,, 6/23/2013 10:25 ,,,,,,,,,,, etc.   

Most of these entries are of value, but I did not want to copy and paste everything. I basically want to sort dates by the latest date / time like Excel.Lambda option is any errors Has been mentioned below because it is reading in the "Time" column for the first time. I have to find out how to leave the first row and on other date / time values ​​properly. Use lambda to sort.

OK, did not fully understand what was going on earlier. The problem you have is that your "date" is still a string and sorting in such a way that I am guessing my date format month / day / year (US date style) as you say that this month Has been sorted by the basis. You have to parse your date in Detach Objects to solve your sorting problem. Import this data from datetime at the top of your file, replace datetime # with sorting your current call: S = sorted (csv.reader (sr), key = lambda X: datetime.strptime (x [0], "% m /% d /% y", reverse = true))

No comments:

Post a Comment