Beginner python - passing variable to imported module

This is the place for queries that don't fit in any of the other categories.

Beginner python - passing variable to imported module

Postby phamp008 » Sat Aug 02, 2014 8:57 pm

Hello,

I have a txt file name "time.txt" with content as below:
Code: Select all
17:00:00
18:00:00


I would like to read from this file and convert time to 12-hr AM/PM format. It doesn't look like I can pass variables between imported module.

Below is my python script.
Code: Select all
#!/usr/bin/env python
import fileinput
import datetime

# read file to convert string to time
for line in fileinput.input('time.txt'):
  time1=datetime.datetime.strptime(line, "%H:%M:%S")
  time3=time1.strftime("%I:%M:%S %p")
  print time3,


thanks,
Last edited by Yoriz on Sat Aug 02, 2014 9:32 pm, edited 1 time in total.
Reason: First post lock, Added code tags.
phamp008
 
Posts: 1
Joined: Sat Aug 02, 2014 8:33 pm

Re: Beginner python - passing variable to imported module

Postby Yoriz » Sat Aug 02, 2014 9:33 pm

Welcome to the forum, Please read the new users read this link in my signature.

Strip the whitespace from the line and it might work ok.

Code: Select all
#!/usr/bin/env python
import fileinput
import datetime

# read file to convert string to time
for line in fileinput.input('time.txt'):
    time1 = datetime.datetime.strptime(line.strip(), "%H:%M:%S")
    time3 = time1.strftime("%I:%M:%S %p")
    print time3,
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: conor100, pnelsonsr and 3 guests