Replace Mathematical Symbols in File - python

I have a file which contains some mathematical expressions in latex form. For example, I have the following which appears in my file:
{\frac{d^{2}}{d^{2}{r}}}\zeta
I would like to write a python code which will scan the file and output a new file where all the instances of the above expression are replaced with
\zeta''
I have tried the following code:
import sys
import fileinput
for line in fileinput.input():
l = line.replace(r"{\frac{d^{2}}{d^{2}{r}}}\zeta","\zeta'")
sys.stdout = open('output.txt','a')
sys.stdout.write(l)
I know that the r which appears just before the first string to be replaced tells the code to ignore any escape characters. But it appears to have difficulty dealing with the d^{2} part. This "^" symbol is not correctly interpreted by the code, so it doesn't make the replacement.
I know that {\frac{d^{2}}{d^{2}{r}}}\zeta is not technically a string, but I'm not sure how else to treat it. Any help would be great. Thanks.

An equivalent of your code (regex.py):
#!/usr/bin/python
import sys
import fileinput
x = open("output.txt", "a")
for line in fileinput.input():
l = line.replace(r"{\frac{d^{2}}{d^{2}{r}}}\zeta","\zeta''")
x.write(l)
Seems to run just fine: $ echo '{\frac{d^{2}}{d^{2}{r}}}\zeta' | ./regex.py gives:
\zeta''

Related

Grep a string in python

Friends,
I have a situation where i need to grep a word from a string
[MBeanServerInvocationHandler]com.bea:Name=itms2md01,Location=hello,Type=ServerRuntime
What I want to grep is the word that assigned to the variable Name in the above string which is itms2md01.
In my case i have to grep which ever string assigned to Name= so there is no particular string i have to search
Tried:
import re
import sys
file = open(sys.argv[2], "r")
for line in file:
if re.search(sys.argv[1], line):
print line,
Deak is right. As I am not having enough reputation to comment, I am depicting it below. I am not going to the file level. Just see as an instance:-
import re
str1 = "[MBeanServerInvocationHandler]com.bea:Name=itms2md01,Location=hello,Type=ServerRuntime"
pat = '(?<=Name=)\w+(?=,)'
print re.search(pat, str1).group()
Accordingly you can apply your logic with the file content with this pattern
I like to use named groups, because I'm often searching for more than one thing. But even for one item in the search, it still works nicely, and I can remember very easily what I was searching for.
I'm not certain that I fully understand the question, but if you are saying that the user can pass a key to search the value for and also a file from which to search, you can do that like this:
So, for this case, I might do:
import re
import sys
file = open(sys.argv[2], "r")
for line in file:
match = re.search(r"%s=(?P<item>[^,]+)" % sys.argv[1], line)
if match is not None:
print match.group('item')
I am assuming that is the purpose, as you have included sys.argv[1] into the search, though you didn't mention why you did so in your question.

Deleting generalised strings from a text file

I am trying to delete strings from a text file. The strings consist of different types of characters and are all different, but they all start with the same three letters and finish at the end of the line.
So far I am using this code, which I know works when I want to delete all occurrences of a specific string:
import sys
import fileinput
for i, line in enumerate(fileinput.input('duck_test.txt', inplace=1)):
sys.stdout.write(line.replace('pep.*', '')
I have tried to adapt it to delete a generalised string using '.*' but it doesn't work. Does anyone know where I am going wrong? Thanks.
Try to use re module for that purpose:
import re
import fileinput
for line in fileinput.input('duck_test.txt', inplace=True):
if not re.search(r'pep.*', line):
sys.stdout.write(line)
The following tested code will replace all strings that begin with the letters 'pep' and end with a newline in the file 'duck_test' with an empty string:
import sys
import fileinput
import re
for i, line in enumerate(fileinput.input('duck_test', inplace=1)):
sys.stdout.write(re.sub(r'pep.*', '', line))

PYTHON : How to find block comments string in Python

I want to parse block comments in python. I order to do that I need to find comments section in .py file.
For example, my sample.py contains
'''
This is sample file for testing
This is sample file for development
'''
import os
from os import path
def somefun():
return
I want to parse the comments section for which I am trying to do following in sampleparsing.py
tempfile = open('sample.py', 'r')
for line in tempfile:
if(line.find(''''') != -1):
dosomeoperation()
break
Since if(line.find(''''') != -1): assumes as remaining lines below as commented how to replace this string to find comments?
I tried keeping '\' (escape character) in between but I could not find out solution for this issue.
I need following two lines after parsing in sampleparsing.py:
This is sample file for testing
This is sample file for development
Try "'''" as your string instead. Right now Python thinks you are still writing a string.
Try this
tempfile = open('sample.py', 'r')
lines = tempfile.readlines()
comments = lines.split( "'''" )
tempfile.close()
Assuming the file starts with a comment block, the comment blocks should now be the odd numbered indices, 1, 3, 5...

Python regex search for string at beginning of line in file

Here's my code:
#!/usr/bin/python
import io
import re
f = open('/etc/ssh/sshd_config','r')
strings = re.search(r'.*IgnoreR.*', f.read())
print(strings)
That returns data, but I need specific regex matching: e.g.:
^\s*[^#]*IgnoreRhosts\s+yes
If I change my code to simply:
strings = re.search(r'^IgnoreR.*', f.read())
or even
strings = re.search(r'^.*IgnoreR.*', f.read())
I don't get anything back. I need to be able to use real regex's like in perl
You can use the multiline mode then ^ match the beginning of a line:
#!/usr/bin/python
import io
import re
f = open('/etc/ssh/sshd_config','r')
strings = re.search(r"^\s*[^#]*IgnoreRhosts\s+yes", f.read(), flags=re.MULTILINE)
print(strings.group(0))
Note that without this mode you can always replace ^ by \n
Note too that this file is calibrated as a tomato thus:
^IgnoreRhosts\s+yes
is good enough for checking the parameter
EDIT: a better way
with open('/etc/ssh/sshd_config') as f:
for line in f:
if line.startswith('IgnoreRhosts yes'):
print(line)
One more time there is no reason to have leading spaces. However if you want to be sure you can always use lstrip().

How can I use readline method?

I have this trivial code:
from sys import argv
script, input_file = argv
def fma(f):
f.readline()
current_file = open(input_file)
fma(current_file)
The contents of the txt file is:
Hello this is a test.\n
I like cheese and macaroni.\n
I love to drink juice.\n
\n
\n
I put the \n chars so you know I hit enter in my text editor.
What I want to accomplish is to get back every single line and every \n character.
The problem is, when running the script I get nothing back. What am I doing wrong and how can I fix it in order to run as I stated above?
Your function reads a line, but does nothing with it:
def fma(f):
f.readline()
You'd need to return the string that f.readline() gives you. Keep in mind, in the interactive prompt, the last value produced is printed automatically, but that isn't how Python code in a .py file works.
Pretty certain what you actually want is f.readlines, not f.readline.
# module start
from __future__ import with_statement # for python 2.5 and earlier
def readfile(path):
with open(path) as src:
return src.readlines()
if __name__ == '__main__':
import sys
print readfile(sys.argv[1])
# module end
Note that I am using the with context manager to open your file more efficiently (it does the job of closing the file for you). In Python 2.6 and later you don't need the fancy import statement at the top to use it, but I have a habit of including it for anyone still using older Python.
def fma(f):
f.readline()
f.readline() is a function, it returns a value which is in this case a line from the file, you need to "do" something with that value like:
def fma(f):
print f.readline()
Your function is not returning anything.
def fma(f):
data = f.readline()
return data
f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline.
the script should be as follow:
from sys import argv
script, input_file = argv
def fma(f):
line = f.readline()
return line
current_file = open(input_file)
print fma(current_file)

Categories

Resources