I want to search a line and replace a line in a file.txt,
CURRENT_BUILD = 111111
with
CURRENT_BUILD = 221111
Using Python.
You can iterate through the lines in the file, saving them to a list and then output the list back to the file, line by line, replacing the line if necessary:
with open('file.txt') as f:
lines = f.readlines()
with open('file.txt', 'w+') as f:
for line in lines:
if line == 'CURRENT_BUILD = 111111\n':
f.write('CURRENT_BUILD = 221111\n')
else:
f.write(line)
CURRENT_BUILD = '111111'
print (CURRENT_BUILD.replace('111111', '221111'))
The syntax of replace() is:
str.replace(old, new [, count])
old - old substring you want to replace
new - new substring which would replace the old substring
count (optional) - the number of times you want to replace the old substring with the new substring
If count is not specified, replace() method replaces all occurrences of the old substring with the new substring.
I'm not sure if this is what you meant as you are unclear and haven't shown what the .txt file is but this is replace anyways.
EDIT
if it is in text replace you want then this would be your best bet.
import fileinput
with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(text_to_search, replacement_text), end='')
credit to #jfs from this post How to search and replace text in a file using Python?
Related
I am trying to find a line starts with specific string and replace entire line with new string
I tried this code
filename = "settings.txt"
for line in fileinput.input(filename, inplace=True):
print line.replace('BASE_URI =', 'BASE_URI = "http://example.net"')
This one not replacing entire line but just a matching string. what is best way to replace entire line starting with string ?
You don't need to know what old is; just redefine the entire line:
import sys
import fileinput
for line in fileinput.input([filename], inplace=True):
if line.strip().startswith('BASE_URI ='):
line = 'BASE_URI = "http://example.net"\n'
sys.stdout.write(line)
Are you using the python 2 syntax. Since python 2 is discontinued, I will try to solve this in python 3 syntax
suppose you need to replace lines that start with "Hello" to "Not Found" then you can do is
lines = open("settings.txt").readlines()
newlines = []
for line in lines:
if not line.startswith("Hello"):
newlines.append(line)
else:
newlines.append("Not Found")
with open("settings.txt", "w+") as fh:
for line in newlines:
fh.write(line+"\n")
This should do the trick:
def replace_line(source, destination, starts_with, replacement):
# Open file path
with open(source) as s_file:
# Store all file lines in lines
lines = s_file.readlines()
# Iterate lines
for i in range(len(lines)):
# If a line starts with given string
if lines[i].startswith(starts_with):
# Replace whole line and use current line separator (last character (-1))
lines[i] = replacement + lines[-1]
# Open destination file and write modified lines list into it
with open(destination, "w") as d_file:
d_file.writelines(lines)
Call it using this parameters:
replace_line("settings.txt", "settings.txt", 'BASE_URI =', 'BASE_URI = "http://example.net"')
Cheers!
I have a text file that has a sequence of four characters a,b,c,d which is 100 lines long that I want to convert to a text string.
There are lines in the txt file that have asterisks that I want to skip entirely.
Here is an example of how the txt file can look. Note the third row has an asterisk where I want to skip the entire row
abcddbabcbbbdccbbdbaaabcbdbab
bacbdbbccdcbdabaabbbdcbababdb
bccddb*bacddcccbabababbdbdbcb
Below is how I'm trying to do this.
s = ''
with open("letters.txt", "r") as letr:
for line in letr:
if '*' not in line:
s.join(line)
Need to use readlines() function.
This is an example, please modify it yourself.
s = ''
with open("letters.txt", "r") as letr:
result = letr.readlines()
print(result)
for line in result:
if '*' not in line:
s += line
print(line)
print(s)
I looked at other answers and found that I made a mistake, your code s.join(line) --> s += line is ok.
s = ''
with open("letters.txt", "r") as letr:
for line in letr:
if '*' not in line:
s += line
builtin type str.method return a string which is the concatenation of the strings in iterable. you should use s += line for contacting string one by one.
Iterate a text file is not a problem.
I have a function that loops through a file that Looks like this:
"#" XDI/1.0 XDAC/1.4 Athena/0.9.25
"#" Column.4: pre_edge
Content
That is to say that after the "#" there is a comment. My function aims to read each line and if it starts with a specific word, select what is after the ":"
For example if I had These two lines. I would like to read through them and if the line starts with "#" and contains the word "Column.4" the word "pre_edge" should be stored.
An example of my current approach follows:
with open(file, "r") as f:
for line in f:
if line.startswith ('#'):
word = line.split(" Column.4:")[1]
else:
print("n")
I think my Trouble is specifically after finding a line that starts with "#" how can I parse/search through it? and save its Content if it contains the desidered word.
In case that # comment contain str Column.4: as stated above, you could parse it this way.
with open(filepath) as f:
for line in f:
if line.startswith('#'):
# Here you proceed comment lines
if 'Column.4' in line:
first, remainder = line.split('Column.4: ')
# Remainder contains everything after '# Column.4: '
# So if you want to get first word ->
word = remainder.split()[0]
else:
# Here you can proceed lines that are not comments
pass
Note
Also it is a good practice to use for line in f: statement instead of f.readlines() (as mentioned in other answers), because this way you don't load all lines into memory, but proceed them one by one.
You should start by reading the file into a list and then work through that instead:
file = 'test.txt' #<- call file whatever you want
with open(file, "r") as f:
txt = f.readlines()
for line in txt:
if line.startswith ('"#"'):
word = line.split(" Column.4: ")
try:
print(word[1])
except IndexError:
print(word)
else:
print("n")
Output:
>>> ['"#" XDI/1.0 XDAC/1.4 Athena/0.9.25\n']
>>> pre_edge
Used a try and except catch because the first line also starts with "#" and we can't split that with your current logic.
Also, as a side note, in the question you have the file with lines starting as "#" with the quotation marks so the startswith() function was altered as such.
with open('stuff.txt', 'r+') as f:
data = f.readlines()
for line in data:
words = line.split()
if words and ('#' in words[0]) and ("Column.4:" in words):
print(words[-1])
# pre_edge
Please see following attached image showing the format of the text file. I need to extract the dimensions of data matrix indicated by the first line in the file, here 49 * 70 * 1 for the case shown by the image. Note that the length of name "gd_fac" can be varying. How can I extract these numbers as integers? I am using Python 3.6.
Specification is not very clear. I am assuming that the information you want will always be in the first line, and always be in parenthesis. After that:
with open(filename) as infile:
line = infile.readline()
string = line[line.find('(')+1:line.find(')')]
lst = string.split('x')
This will create the list lst = [49, 70, 1].
What is happening here:
First I open the file (you will need to replace filename with the name of your file, as a string. The with ... as ... structure ensures that the file is closed after use. Then I read the first line. After that. I select only the parts of that line that fall after the open paren (, and before the close paren ). Finally, I break the string into parts, with the character x as the separator. This creates a list that contains the values in the first line of the file, which fall between parenthesis, and are separated by x.
Since you have mentioned that length of 'gd_fac' van be variable, best solution will be using Regular Expression.
import re
with open("a.txt") as fh:
for line in fh:
if '(' in line and ')' in line:
dimension = re.findall(r'.*\((.*)\)',line)[0]
break
print dimension
Output:
'49x70x1'
What this does is it looks for "gd_fac"
then if it's there is removes all the unneeded stuff and replaces it with just what you want.
with open('test.txt', 'r') as infile:
for line in infile:
if("gd_fac" in line):
line = line.replace("gd_fac", "")
line = line.replace("x", "*")
line = line.replace("(","")
line = line.replace(")","")
print (line)
break
OUTPUT: "49x70x1"
I am trying to count number of occurrences of a word from a file, using Python. But I have to ignore comments in the file.
I have a function like this:
def getWordCount(file_name, word):
count = file_name.read().count(word)
file_name.seek(0)
return count
How to ignore where the line begins with a # ?
I know this can be done by reading the file line by line like stated in this question. Are there any faster, more Pythonian way to do so ?
You can use a regular expression to filter out comments:
import re
text = """ This line contains a word. # empty
This line contains two: word word # word
newline
# another word
"""
filtered = ''.join(re.split('#.*', text))
print(filtered)
# This line contains a word.
# This line contains two: word word
# newline
print(text.count('word')) # 5
print(filtered.count('word')) # 3
Just replace text with your file_name.read().
You can do one thing just create a file that is not having the commented line then run your code Ex.
infile = file('./file_with_comment.txt')
newopen = open('./newfile.txt', 'w')
for line in infile :
li=line.strip()
if not li.startswith("#"):
newopen.write(line)
newopen.close()
This will remove every line startswith # then run your function on newfile.txt
def getWordCount(file_name, word):
count = file_name.read().count(word)
file_name.seek(0)
return count
More Pythonian would be this:
def getWordCount(file_name, word):
with open(file_name) as wordFile:
return sum(line.count(word)
for line in wordFile
if not line.startswith('#'))
Faster (which is independent from being Pythonian) could be to read the whole file into one string, then use regexps to find the words not in a line starting with a hash.