How to replace string after specific character using python - python

How to replace string after specific character using python
I have a file with below contents
The Test file contents are as below "Test1"{
Serial = 12345
IP = 12.10.23.10
User = user1
}
how do i replace the contents after the = symbol using python ?
for example i want to replace with below contents.
The Test file contents are as below "Test1"{
Serial = 22330011
IP = 1.1.1.1
User = User_11
}
The contents after = symbols are not pre defined, hence substituting 12345 with 22330011 is not required here.
need a logic to find what is there after = symbol and replace it with the user defined value.

Lets say i have above data in temp.txt
file=open('temp.txt','r')
data=file.readlines()
outdata=[]
for line in data:
try:
lhs,rhs=line.split('=')
rhs=input()
outdata.append('='.join([lhs,' '+rhs,'\n']))
except:
outdata.append(line)
file.close()
file=open('temp.txt','w')
for line in outdata:
file.write(line)
This code read from the file and ask the input from user for rhs and updates in the file again

Related

regex to grep string from config file in python

I have config file which contains network configurations something like given below.
LISTEN=192.168.180.1 #the network which listen the traffic
NETMASK=255.255.0.0
DOMAIN =test.com
Need to grep the values from the config. the following is my current code.
import re
with open('config.txt') as f:
data = f.read()
listen = re.findall('LISTEN=(.*)',data)
print listen
the variable listen contains
192.168.180.1 #the network which listen the traffic
but I no need the commented information but sometimes comments may not exist like other "NETMASK"
If you really want to this using regular expressions I would suggest changing it to LISTEN=([^#$]+)
Which should match anything up to the pound sign opening the comment or a newline character.
I come up with solution which will have common regex and replace "#".
import re
data = '''
LISTEN=192.168.180.1 #the network which listen the traffic
NETMASK=255.255.0.0
DOMAIN =test.com
'''
#Common regex to get all values
match = re.findall(r'.*=(.*)#*',data)
print "Total match found"
print match
#Remove # part if any
for index,val in enumerate(match):
if "#" in val:
val = (val.split("#")[0]).strip()
match[index] = val
print "Match after removing #"
print match
Output :
Total match found
['192.168.180.1 #the network which listen the traffic', '255.255.0.0', 'test.com']
Match after removing #
['192.168.180.1', '255.255.0.0', 'test.com']
data = """LISTEN=192.168.180.1 #the network which listen the traffic"""
import re
print(re.search(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}', data).group())
>>>192.168.180.1
print(re.search(r'[0-9]+(?:\.[0-9]+){3}', data).group())
>>>192.168.180.1
In my experience regex is slow runtime and not very readable. I would do:
with open('config.txt') as f:
for line in f:
if not line.startswith("LISTEN="):
continue
rest = line.split("=", 1)[1]
nocomment = rest.split("#", 1)[0]
print nocomment
I think the better approach is to read the whole file as the format it is given in. I wrote a couple of tutorials, e.g. for YAML, CSV, JSON.
It looks as if this is an INI file.
Example Code
Example INI file
INI files need a header. I assume it is network:
[network]
LISTEN=192.168.180.1 #the network which listen the traffic
NETMASK=255.255.0.0
DOMAIN =test.com
Python 2
#!/usr/bin/env python
import ConfigParser
import io
# Load the configuration file
with open("config.ini") as f:
sample_config = f.read()
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))
# List all contents
print("List all contents")
for section in config.sections():
print("Section: %s" % section)
for options in config.options(section):
print("x %s:::%s:::%s" % (options,
config.get(section, options),
str(type(options))))
# Print some contents
print("\nPrint some contents")
print(config.get('other', 'use_anonymous')) # Just get the value
Python 3
Look at configparser:
#!/usr/bin/env python
import configparser
# Load the configuration file
config = configparser.RawConfigParser(allow_no_value=True)
with open("config.ini") as f:
config.readfp(f)
# Print some contents
print(config.get('network', 'LISTEN'))
gives:
192.168.180.1 #the network which listen the traffic
Hence you need to parse that value as well, as INI seems not to know #-comments.

Why is my tkinter program writing numbers to a file instead of the actual values?

I've created a tkinter program that writes the user's data to a file, but when writing the variable they turn into numbers.
This is what is being written to the file:
.15212688
.15213328
.15213232
INVALID REGISRATION NUMBER
And this is my code:
def show_entry_fields():#creates a function called show entry fields.
print("First Name: %s\nLast Name: %s\nRegistration number:%s" % (Fname.get(), Lname.get(), reg.get()))
if re.match('^[A-Za-z]{2}[0-9]{2}[A-Za-z]{3}$', reg.get()):#validates the users reg number by ching that its in the correct format
w.configure(text='That is a valid registration number')# if the reg is valid, tells user
else:
w.configure(text='Invalid registration number')#user is told that reg is not valid
data = open("invalid.txt", "w")#users data is writen to a file called 'invalid'
data.write(str(Fname))#first name
data.write('\n')#new line
data.write(str(Lname))#last name
data.write('\n')#new line
data.write(str(reg))# the casr's regitration number
data.write('\n')#new line
data.write('INVALID REGISRATION NUMBER')# the perpiose of being writen to the file
data.close()#closes the file when finished.
You should use
data.write(str(Fname.get()))#first name
data.write('\n')#new line
data.write(str(Lname.get()))#last name
data.write(str(reg.get()))
In your code you're converting objects to strings.

Python Parse log file string into txt file

I have written a simple Python Script that parses a log and prints a string if a line is equal to a mac address.
My next step is to parse out the log file into a txt file.
Is there a way to create a variable for each mac address of the access point so that when it parses out into text file, it will not show mac address it will show the AP name, and user name?
here is an example line
[2014-07-22 10:21:06,821] <inform_stat-3> WARN event - [event] User[78:a3:e4:3b:bd:3d] roams from AP[dc:9f:db:1a:60:64] to AP[dc:9f:db:1a:2d:95] on "channel 11(ng)"
My code is currently as follows
for line in open("system.log"):
if "e4:98:d6:27:4c:b6" in line:
print line
Start with the dictionary:
dd = {'78:a3:e4:3b:bd:3d' : 'Joe Smith',
'dc:9f:db:1a:60:64' : 'Server1',
'dc:9f:db:1a:2d:95' : 'Server1'}
dat = '[2014-07-22 10:21:06,821] <inform_stat-3> WARN event - [event] User[78:a3:e4:3b:bd:3d] roams from AP[dc:9f:db:1a:60:64] to AP[dc:9f:db:1a:2d:95] on "channel 11(ng)"'
aps = []
for elem in dat.split():
if elem.startswith('User['):
uname = dd.get(elem[5:-1], 'Unknown')
if elem.startswith('AP['):
aps.append(dd.get(elem[3:-1], 'Unknown'))
now you should have enough information to print to the text file

Drop Down Menu In Python calling information from files

So I need a drop down menu, where the user picks his/her client, and it returns information about that client.
lets say i have a file:
["client1", "client2", "client3"]
and I have this code:
from tkinter import *
master = Tk()
with open('ubclientlistvars.txt', 'r') as clients:
clients = (clients.readlines())
variable = StringVar(master)
variable.set("Choose Client")
w = OptionMenu(master, variable, clients)
w.pack()
mainloop()
how would I draw the clients from the file into the drop down menu?
When I run this code, i get these two options:
Choose Client and {["client1", "client2", "client3"]}
You need to actually parse that file. If the file contents are what you posted, then readlines() is just returning a single line of text. It does not magically convert the file contents into a Python object. Suppose the file was:
client1
client2
client3
Then you could use something like clients = [i.strip() for i in f.readlines()] to get a proper list of clients and can pass them to OptionMenu:
w = OptionMenu(master, variable, *clients)
If you cannot change the file format then you will need to clean up the input before displaying it...
import re
data = f.read() # ["client1", "client2", "client3"]
data = re.sub('["\[\]]', '', data) # remove the ", [, and ] characters
clients = data.split(',') # split the list of clients on the comma

Writing a txt file after using pylast - encoding error

I am having problems writing to a file where I am using pylast. Following a template given in pylast, I added a regular expression to extract what I need (which is doing ok), but when I tried to print to a file, I get an error, and don't know how to fix it (I am teaching myself python and some of its libraries).
I suspect there is an encoding specification I need to make somewhere (some of the output to screen also shows non-standard characters). I don't know how to solve my problem.
Can anybody help?
Thanks
import re
import pylast
RawArtistList = []
ArtistList = []
# You have to have your own unique two values for API_KEY and API_SECRET
# Obtain yours from http://www.last.fm/api/account for Last.fm
API_KEY = "XXX"
API_SECRET = "YYY"
###### In order to perform a write operation you need to authenticate yourself
username = "username"
password_hash = pylast.md5("password")
network = pylast.LastFMNetwork(api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash)
## _________INIT__________
COUNTRY = "Germany"
#---------------------- Get Geo Country --------------------
geo_country = network.get_country(COUNTRY)
#---------------------- Get artist --------------------
top_artists_of_country = str(geo_country.get_top_artists())
RawArtistList = re.findall(r"u'(.*?)'", top_artists_of_country)
top_artists_file = open("C:\artist.txt", "w")
for artist in RawArtistList:
print artist
top_artists_file.write(artist + "\n")
top_artists_file.close()
The name of the file I am trying to create "artist.txt" changes to "x07rtist.txt" and the error kicks in. I get this:
Traceback (most recent call last):
File "C:\music4A.py", line 32, in <module>
top_artists_file = open("C:\artist.txt", "w")
IOError: [Errno 22] invalid mode ('w') or filename:'C:\x07rtist.txt'
Thank you very much for any help! Cheers.
The Python docs say:
The backslash () character is used to escape characters that
otherwise have a special meaning, such as newline, backslash itself,
or the quote character.
...so when you say
top_artists_file = open("C:\artist.txt", "w")
that string literal is being interpreted as
C: \a rtist.txt
...where \a is a single character that has a value of 0x07.
...that line should instead be:
# doubling the backslash prevents misinterpreting the 'a'
top_artists_file = open("C:\\artist.txt", "w")
or
# define the string literal as a raw string to prevent the escape behavior
top_artists_file = open(r"C:\artist.txt", "w")
or
# forward slashes work just fine as path separators on Windows.
top_artists_file = open("C:/artist.txt", "w")

Categories

Resources