Miika
Posts: 3
Joined: Fri Jan 31, 2014 5:56 pm

Reading lines backwards?

Fri Jan 31, 2014 6:01 pm

Hi, I was wondering if there was a way to read lines backwards in Python?

I have a program that outputs data in plain txt format as:
NO,Temp,Time
1,-12,63,2014-01-29 23:00:50
2,-12,63,2014-01-29 23:01:49
3,-12,63,2014-01-29 23:02:49


And it would be easier to read the lines backwards to change the date order (D/M/Y) as the character length from the
end of the line is equal until the line number with following comma.

Or are there any simple way to: ditch the line number with following comma, replace the comma "," between -12,63 with "." and
then change the Y-M-D to D.M.Y?`

Thanks for any help =).

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Reading lines backwards?

Fri Jan 31, 2014 7:09 pm

Use String.split to split on commas, and the parse and format methods in the datetime module for, well, parsing and formatting dates.

tldr;

Code: Select all

input_string = "1,-12,63,2014-01-29 23:00:50"
args = [x.strip() for x in input_string.split(',')]
dt = datetime.datetime.strptime(args[3], "%Y-%m-%d %H:%M:%S")
print dt.strftime("%d.%m.%Y") 
[EDIT: fixed typo]
Last edited by Douglas6 on Sun Feb 02, 2014 7:08 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Reading lines backwards?

Fri Jan 31, 2014 7:20 pm

The best thing with (ISO) yyyy-mm-dd format dates is that they naturally sort in the correct order. As soon as you convert those to dd/mm/yyyy or [the ludicrous and confusing] mm/dd/yyyy formats then you lose the natural sorting.

Stick the data in a database (rather than a flat file) and you can use
select date_format(datetime_column, "%D %M %Y) from temp_table [where ...some predicate here ...] order by datetime_column;
to get the best of both worlds (formatting & sorting).

http://dev.mysql.com/doc/refman/5.1/en/ ... ate-format
it uses the same format strings as PHP and python.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Miika
Posts: 3
Joined: Fri Jan 31, 2014 5:56 pm

Re: Reading lines backwards?

Sun Feb 02, 2014 5:14 am

Problem with split is that I haven't found a way to drop or change characters with it.
Like in my example:
1,-12,63,2014-01-29 23:00:50
2,-12,63,2014-01-29 23:01:49
3,-12,63,2014-01-29 23:02:49

I'd like to change that into:
-12.63,2014-01-29,23:02:50
-12.63,2014-01-29,23:01:49
-12.63,2014-01-29,23:02:49

If you can use split so that each splitted part could be assigned to a variable, then this would be piece of cake.
Something like split(","), a$, b$, c$. Problem is with the date/time as it is devided with space, but you could always run the split command to a file second time with split() 8-) .

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Reading lines backwards?

Sun Feb 02, 2014 5:29 am

Miika wrote:If you can use split so that each splitted part could be assigned to a variable
Actually, that's just what the fragment I posted does (well, apart from the typo...fixed now) The args list will have one element for each part, so args[0] = the line number, args[1] and args[2] have the whole and fractional temperature parts, and args[3] has the date-time string. It looks funny (to me) but that's Python for you, and it's actually kinda cool.

You want to keep the date and time together in one datetime object, and then format it any way you like (although it' s best to keep it in ISO format, which it was to begin with) . Take a closer look at the code fragment, read the Python references I mentioned, and play around with it to better understand.

tl;dr:

Code: Select all

print("%s.%s,%s" % (args[1], args[2], dt.strftime("%Y-%m-%d,%H:%M:%S")) 

Miika
Posts: 3
Joined: Fri Jan 31, 2014 5:56 pm

Re: Reading lines backwards?

Tue Feb 11, 2014 3:33 am

Thank you =). I was finally able to make it work to write that file as comma separated for Excel.

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Reading lines backwards?

Tue Feb 11, 2014 3:58 am

Excellent. We always like to hear when things work.

Return to “Python”