You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
 
Thread Tools Display Modes
Old January 10th, 2018, 10:33 AM   #1
the_msp
Lifetime Supporting Member
United Kingdom

the_msp is offline
 
the_msp's Avatar
 
Join Date: May 2008
Location: Northern Ireland
Posts: 610
OT: Python help

I have a text file with machine data, wanting to parse through it and extract PCB ID and date time.

The string I'm interested in is:

Quote:
DataFile contents: Process= Servo Press PCBID= **************** TestTime= 22/12/2017 10:36:52 TestCell
I'm interested in the first 3 digits of the PCB ID and the TestTime

Code:
string = "DataFile contents: Process= Servo Press*TestCell"
file = open(filepath)
			for line in file:
				if line.find(string) != -1:
					pos1 = string.find(string) + 46
					pos2 = string.find(string) + 49
					pos3 = string.find(string) - 19
					pos4 = string.find(string) - 1
					PCB= string[pos1:pos2]
					DT = string[pos3:pos4]
This is my current approach but PCB ID and datetime naturally change with each part. How can I wildcard in so it starts at DataFile, ends at TestCell, and I can count from the start and end point to extract the data I want?

Or is there a better way?
__________________
Regards,

Patrick

https://www.youtube.com/user/MrMatrixEngineering
  Reply With Quote
Old January 10th, 2018, 12:17 PM   #2
dmroeder
Lifetime Supporting Member
United States

dmroeder is offline
 
dmroeder's Avatar
 
Join Date: Apr 2006
Location: Vancouver, WA
Posts: 1,995
Hmm, my first thought is to use split, but it makes some assumptions that the line in the file will have that exact format. My "test.txt" contains the exact line you quoted above:

Code:
with open('test.txt') as f:
    for line in f:
        data = line.split('=')
        pcbid = data[2].split(' ')[1][:3]
        datetime = data[3].split(' ')
        timestamp = datetime[1] + ' ' + datetime[2]
        print pcbid, timestamp
I wouldn't mind looking at an example text file if you want to PM one to me.
  Reply With Quote
Old January 10th, 2018, 12:21 PM   #3
mts5096
Member
United States

mts5096 is offline
 
Join Date: May 2013
Location: Dallastown, PA
Posts: 15
The best way I can think of to do this is with a regular expression, but if you're not familiar with them there's a bit a of learning curve.

Here's a quick and dirty example that will pull the date and time out of your example string (written for python 2):

Code:
import re

s = "DataFile contents: Process= Servo Press PCBID= **************** TestTime= 22/12/2017 10:36:52 TestCell "

date = re.search(r"[0-9]+/[0-9]+/[0-9]+ [0-9]+:[0-9]+:[0-9]+", s)

if date:
  print 'Found a date: %s' % s[date.start(0):date.end(0)]
else:
  print 'not found'
A similar pattern can be used to find the PCBID.
  Reply With Quote
Old January 10th, 2018, 02:56 PM   #4
the_msp
Lifetime Supporting Member
United Kingdom

the_msp is offline
 
the_msp's Avatar
 
Join Date: May 2008
Location: Northern Ireland
Posts: 610
Thanks, I'll give both of these a try tomorrow morning. I was actually following a site after I posted this that was tutoring regular expressions, but no, I've never used them before so didn't get it sorted in the 20 mins I had left.
__________________
Regards,

Patrick

https://www.youtube.com/user/MrMatrixEngineering
  Reply With Quote
Old January 11th, 2018, 02:25 PM   #5
SeanT
Member
United States

SeanT is offline
 
Join Date: Jan 2015
Location: NY
Posts: 6
If you are able to, I would change the format of the data file to give yourself unique delimiters between fields. Right now it looks like you have spaces, but they can also be present in the value. What if you have a process in your data file with only one word in it, instead of "Servo Press," now your split based on number of spaces will be off for everything following the process. A unique delimiter will let you split the keys and values correctly and then you can search the fields based on the name instead of raw position, which in my opinion makes errors less likely.
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
IDEC SmartRelay Web Server and Python Communication JeffreyPeh LIVE PLC Questions And Answers 1 September 22nd, 2016 07:16 AM
Kivy + Python + PLC + RPi DINT2 LIVE PLC Questions And Answers 7 September 9th, 2016 10:28 AM
Exchange data with Logix based PLC's via Python dmroeder LIVE PLC Questions And Answers 17 July 26th, 2016 03:53 PM
OT: WAY OT... How small we are geniusintraining LIVE PLC Questions And Answers 61 June 18th, 2014 10:14 AM
OT: Way OT geniusintraining LIVE PLC Questions And Answers 25 June 17th, 2010 02:28 AM


All times are GMT -5. The time now is 04:06 AM.


.