Why replacing strings from dictionary produce empty file - python

I'm trying to replace some strings with other strings in a text file,
but the code produce empty file (file size is 0)
what am I missing ?
emotion_list = {":-)" : "happy-similey", \
":-(": "sad-similey"}
for line in fileinput.input(file_name, inplace=True):
if not line:
continue
for f_key, f_value in emotion_list.items():
if f_key in line:
line = line.replace(f_key, f_value)

You missing the print statement to send replaced line to your file:
for line in fileinput.input(file_name, inplace=True):
if not line:
continue
for f_key, f_value in emotion_list.items():
if f_key in line:
line = line.replace(f_key, f_value)
print(line, end="") # print without newline

In your code you are maching file line with word.So split line by spaces to get words (If you do match word in a entire line then you have to give the no.of occurences you want to replace and no of occurences is dynamic.You don't how much occurences would be there in file in a real scenarios)
emotion_list = {":-)" : "happy-similey", \
":-(": "sad-similey"}
file="I am not really :-) but I am not :-( too "
for line in file.split():
for f_key, f_value in emotion_list.items():
if f_key == line:
file=file.replace(line, f_value,1)
print(file)
output
I am not really happy-similey but I am not sad-similey too

This is basically the problem you are facing:
lst = ["abc", "acd", "ade"]
for x in lst:
x = x.replace("a", "x")
print(lst) # ["abc", "acd", "ade"]
Instead, you should replace the ith element of the list:
lst = ["abc", "acd", "ade"]
for i, x in enumerate(lst):
lst[i] = x.replace("a", "x")
print(lst) # ['xbc', 'xcd', 'xde']
This is happening because strings are immutable in Python!

Related

How to delete a textline with matched text in QPlainTextEdit?

Below lines are in A QPlainTextEdit:
I want to delete a matched line with line variable help.
For example, I want to delete
line 2 s44 grade
with the help of line variable(line = "line 2")
I am able to delete the particular text with below code:
item = "line 2"
text = self.project_length_lanes_plainedit.toPlainText()
text = text.replace(item, '')
_list = text.split()
text = '\n'.join(_list)
self.project_length_lanes_plainedit.setPlainText(text)
but I want to delete the entire line. How can I do that?
you have 2 basic options:
use regexp to match whole line in your replace, then splitting lines is not necessary
split to list first and remove matching line from list, and in the end join list as you did
EDIT:
import re
x = """
line1: abc
line2: def
line3: ghi
"""
print("regex:")
print(re.sub(r'line2.*', '', x)) # note: this leaves empty line
print("regex2:")
print(re.sub(r'line2.*\n', '', x))
print("list:")
print('\n'.join([line for line in x.split('\n') if "line2" not in line]))
I tried as below working:
item = "line 2"
text = self.project_length_lanes_plainedit.toPlainText()
for l in text.split('\n'):
if item in l:
text = text.replace(l, '')
_list = text.split('\n')
_list = [ i for i in _list if i ]
text = '\n'.join(_list)
self.project_length_lanes_plainedit.setPlainText(text)

Why is my code not showing any output? I am trying to use while loop to debug the error i was getting before it

f = file.readlines()
l = 0
while l <= len(f):
for i in range(l):
x = f[i]
l += 1
for a in x:
if a == "a":
f.pop(i)
break
else:
continue
print(f)
file.close()
I want to pop any line from the data which has any character 'a' in it.
You don't need to manage your own line counter and iterate over each line
character by character. The file itself is iterable without using readlines, and the in operator tells you at once if "a" is a character in a given line.
with open("filename") as f:
for line in f:
if "a" in line:
print(line, end="") # line already ends with a newline
Im not quite understanding the way your code is supposed to work, but this would solve your problem too:
f = file.readlines()
for line in reversed(f):
if "a" in line:
f.remove(line)
for i in range(l) when l is zero will cause the loop to run zero times.

Python search for patterns in all lines, export only lines with results

I would like to search for strings that match a pattern in a text file and export only the matched strings
k=''
regex = re.compile(r'[a-zA-Z]{2}\d{8}')
with open(file, 'r') as f:
for line in f:
line = line.replace(',', '')
line = line.replace('.', '')
k = regex.findall(line)
#k.append(line)
if not k=='':
position=True
else:
position=False
if position==True:
print(k)
Somehow my code doesn't work, it always returns the following output:
[] [] [] [] [] [] [] ['AI13933231'] [] [] [] [] []
I want the output to contain only the matched strings. Thank you!
The reason why there are empty array literals [] is because this line actually exists, but is either empty (containing just \n) or does not match the regex '[a-zA-Z]{2}\d{8}'. And please note that regex.findall(line) returns an list, so if the regex did not find any that matches, it is an empty list.
Your main error happened in this section: if not k=='':. Note k is an list.
Consider this code:
import re
k=''
regex = re.compile(r'[a-zA-Z]{2}\d{8}')
with open("omg.txt", 'r') as f:
for line in f:
line = line.replace(',', '')
line = line.replace('.', '')
k = regex.findall(line)
#k.append(line)
position = False
if str(k) != '[]': # The `[]` is just the string representation of an empty array
position=True
print(k)
else:
position=False
Given the file (Text after # are ignored, not part of the file)
AZ23153133
# Empty line
AB12355342
gz # No match
XY93312344
The output would be
['AZ23153133']
['AB12355342']
['XY93312344']

Check if a string from a list is present in a line Python

I have a list of words ['Ip', 'Name', 'Error']. Reading a log file,
the script should test if that line contains one of the words from the list.
Didn't succeed with 'if list in line' ... any idea ?
This might help. Iterate over your check list and see it element of list in in line.
Ex:
for i in ['Ip', 'Name', 'Error']:
if i in line:
print "Ok!!!!"
You could try something like this.
list(set(line.split()) & set(my_list))
This returns a list of the words present in both the line and the list.
Hope that helps.
Check this out.
def getIndexLinesOfWords(filename,word_list):
infile = open(filename, 'r')
dct = {}
count = 0
for line in infile:
count += 1
newLine = line.replace('\n', ' ')
if newLine == ' ':
continue
split_line = newLine.split()
for word in word_list:
if word in split_line:
if word in dct:
dct[word] += [count]
else:
dct[word] = [count]
for word in word_list:
print('{:12} {}'.format(word,dct[word]))
infile.close()
filename = "C:/Users/fff/Desktop/log.txt"
word_list = ["lp", "error", "name"]
getIndexLinesOfWords(filename,word_list)
You can try use filter :
data=['Ip', 'Name', 'Error']
print(list(filter(lambda x:[True for line in open('file.txt','r') if x in line],data)))

Python regex string match from file

I have this a text file that resembles
alpha alphabet alphameric
I would like to match just the first string `alpha', nothing else
I have the following code that attempts to match just the alpha string and get its line number
findWord = re.findall('\\ba\\b', "alpha")
with open(file) as myFile:
for num, line in enumerate(myFile, 1):
if findWord in line:
print 'Found at line: ', num
However I get the following error:
TypeError: 'in ' requires string as left operand, not list
Issues in your code
re.findall('\\ba\\b', "alpha") gives a matched list but you are using in if findWord in line means using list in place of string . That's what the error you are getting
By giving findWord = re.findall('\\ba\\b', "alpha") you are searching for string a in alpha string which is not existing
Try this
import re
#findWord = re.findall('\\ba\\b', "alpha")
#print findWord
with open("data.txt") as myFile:
for num,line in enumerate(myFile):
if re.findall('\\balpha\\b', line):
print 'Found at line: ', num+1
You may modify your code a bit
with open(file, 'r') as myFile:
for num, line in enumerate(myFile, 1):
if 'alpha' in line.split():
print 'Found at line', num
Output:
Found at line 1
You can try this:
import re
s = "alpha alphabet alphameric"
data = re.findall("alpha(?=\s)", s)[0]
Output:
"alpha"

Categories

Resources