Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Basically I want a Python script to search a .txt for any line containing,
" #1111. "
1111 < = any number from 0-9, so any possibility 0-9 with 4 numbers, containing # at the start and . at the end.
You'll want to use what's called a Regular Expression.
Python has a regular expression module called re.
import re
with open('file.txt', 'r') as f:
matches = [line for line in f if re.search(r'#\d{4}\.', line)]
print matches
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want the data enclosed between three asterisks.And the Word should start with description.
For eg:I have data like
description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;
I want only
tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568
You may use re.findall here:
inp = "description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;"
matches = re.findall(r'\bdescription\s+\*{3}(.*?)\*{3}', inp, flags=re.DOTALL)
print(matches)
This prints:
['tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568']
Note that I use dot all mode in the regex, in case your expected matches might span across more than one line.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am getting into file handling in python and want to read the info in a file. How do I read the file without additional lines?
Here's my code:
data = f.readlines()
for line in data:
print(line)
Incorporating falsetru's point,
for line in f:
if line.strip():
print(line)
The strip command will strip all white space, and at that point, if you have an empty string, the condition will fail.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
consider below the output of my subprocess module of python. Now from here I want to grab the very first username only like root ,daemon and continuew. I tried but not able to write the exact regex to fetch the users only .
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
You do not a regex in the first place, as per your comments. So, you may iterate over the output line by line (str.splitlines()), split the line with : (str.split(':')) and take the first result (result[0]). This expects the output to be consistent, else it will fail.
Use split instead of regex
for line in output.split('\n'):
print line.split(':')[0]
>>>root
>>>daemon
....
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to Python and need to remove lines from text file in Python that contain a certain letter. i.e. a text file containing the months of the year and I want to remove the months containing an r. I'm completely lost.
How about this? (Reads from stdin, writes to stdout.)
import sys
for line in sys.stdin:
if 'r' not in line:
print(line)
Sample usage:
$ python program.py < months_file > months_without_r_file
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a file like this:
Hi:
fdsfdsfdsfdsfdsfdsfdsfdsfdfdsfdsfdsfsdfdsfsdfdsfsdfdsfds
fdsfdsfdsfdsfdsfdsfdsfsfdsfdsfsdfsdfsdfsdffdsfdsfds
Exampples:
>>fdsfds
>>ok
This is it.
Hello:
fdsfdsfdsfdsfdsfdsfdsfdsfdsfsd
fdsfdsfdsfdsfds
fdsfdsfsd
The section of Hi is from fds... to This is it. The section of Hello is from fds.. to fds..
I want to get only the section of all the headings. I thought of the following approach:
Start from : and then look upto \n\n which will give me the section respectively. But this won't because the section itself can have the same format. I don't want to do this using regex or Configparser. I am looking for simple parsing. How to tackle this problem?
You could search for lines not starting with five spaces:
tab = " " # five spaces
with open('input.txt', 'r') as f:
for line in f:
if line.startswith(tab):
print line
This is really easy with a regex:
txt='''\
Hi:
fdsfdsfdsfdsfdsfdsfdsfdsfdfdsfdsfdsfsdfdsfsdfdsfsdfdsfds
fdsfdsfdsfdsfdsfdsfdsfsfdsfdsfsdfsdfsdfsdffdsfdsfds
Exampples:
>>fdsfds
>>ok
This is it.
Hello:
fdsfdsfdsfdsfdsfdsfdsfdsfdsfsd
fdsfdsfdsfdsfds
fdsfdsfsd'''
import re
print(re.findall(r'^(\w+:.*?)(?=^\w+:|\Z)', txt, re.S | re.M))
Prints:
['Hi:\n fdsfdsfdsfdsfdsfdsfdsfdsfdfdsfdsfdsfsdfdsfsdfdsfsdfdsfds\n fdsfdsfdsfdsfdsfdsfdsfsfdsfdsfsdfsdfsdfsdffdsfdsfds\n Exampples:\n\n >>fdsfds\n >>ok\n\n This is it.\n\n', 'Hello:\n fdsfdsfdsfdsfdsfdsfdsfdsfdsfsd\n fdsfdsfdsfdsfds\n fdsfdsfsd']