1

system generates a text file. it contains more than 100 lines. i like to get a line in the file.

some text **
Actions Pending are: Action-1, Action-2,....Action-3 (this is another new line)
some text**

need to get the Actions in pending to array.

i used

for index in text:
 rc.logMessage(str(index))

it is printing each character at a time not the line.

help me how can i parse this file to get actions into an array.

Thanks in advance

Jacob
43.6k6 gold badges81 silver badges81 bronze badges
asked Aug 4, 2011 at 11:21

7 Answers 7

4

Something like:

d = """some text **
Actions Pending are: Action-1, Action-2, Action-3
some text**
"""
res = []
for line in re.findall('Actions Pending are: (.+)', d):
 res.extend([action.strip() for action in line.split(',')])
['Action-1', 'Action-2', 'Action-3']
answered Aug 4, 2011 at 11:30
Sign up to request clarification or add additional context in comments.

Comments

3

You can try something like this:

pendingActions = []
textToSearch = 'Actions Pending are:'
for line in open(filename, 'r'):
 line = line.strip()
 if line and line.startswith(textToSearch):
 pendingActions.extend([x.strip() for x in line[len(textToSearch):].split(',') if x.strip()])
answered Aug 4, 2011 at 11:28

Comments

3

You need to iterate over the file, not a string read from the file.

with open(filename) as text:
 for line in text:
 rc.logMessage(some_function_of_the_line(line))

Iterating over the file gives you lines; iterating over a string gives you characters / bytes.

answered Aug 4, 2011 at 11:29

Comments

2

You want str.splitlines() http://docs.python.org/library/stdtypes.html#str.splitlines

for index in text:
 rc.logMessage(str(index))

becomes:

for index in text.splitlines():
 rc.logMessage(str(index))
answered Aug 4, 2011 at 11:30

Comments

1

Try something like this

with file("your_file") as logfile:
 result = [line for line in logfile if line.startswith("Actions pending")]

This way in result you will have all the actions lines.

answered Aug 4, 2011 at 11:31

Comments

0
 search_string = 'Actions Pending are: '
 for line in open('yourfile.txt', 'r').readlines():
 if line.startswith(search_string):
 actions = line[len(search_string):].split(',')
 break
 print actions

Artsiom was faster: parsing text file for a line, in python, maybe my version is more readable.

answered Aug 4, 2011 at 11:31

Comments

0

Here is a one-liner (for fun):

s = """some text **
Actions Pending are: Action-1, Action-2, Action-3
Actions Pending are: Action-4, Action-5, Action-6
some text**"""
[a for ln in s.splitlines() if ln.startswith("Actions Pending") for a in ln[len("Actions Pending are: "):].split(', ')]
------
['Action-1', 'Action-2', 'Action-3', 'Action-4', 'Action-5', 'Action-6']

To use a file instead of a string, replace s.splitlines() with f.readlines(). Note, I wouldn't use this code in practice; It is just for fun.

answered Oct 2, 2013 at 10:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.