replace text with a longer text in a text file on python - python

I have an text file having the content as follows
1 0.374023 0.854818 0.138672 0.230469
0 0.939941 0.597005 0.118164 0.782552
1 0.826118 0.582643 0.347764 0.803151
1 0.503418 0.822266 0.100586 0.240885
I want to replace "1", at the beginning, with "80"
like following:
80 0.374023 0.854818 0.138672 0.230469
0 0.939941 0.597005 0.118164 0.782552
80 0.826118 0.582643 0.347764 0.803151
80 0.503418 0.822266 0.100586 0.240885
keeping the rest of the content same.

Try doing in two step file opening:
with open("a_file.txt","r") as f:
lines = a.readlines()
lines = ["80"+line[1:] if line[0:2]=="1 " else line for line in lines]
#OR
lines =["80"+line[1:] if line.split(maxsplit=1)[0] == "1" else line for line in lines]
with open("a_file.txt","w") as f:
for line in lines:
f.write(line)

If you do not need to use python, then this can be done easily with sed using the following:
sed -i 's/^1/80/g' input_file.txt
The s/<regex>/<replacement>/g means replace all occurrences of <regex> with <replacement>. The ^1 is a regular expression that means "match any '1' at the beginning of the line".
Alternatively, the following python code will do the same thing:
file = open("input_file.txt")
lines = file.readlines()
outFile = open("input_file.txt", "w")
for line in lines:
split = line.strip().split(" ")
split[0] = 80 if (split[0] == "1") else split[0]
print(*split, file=outFile)
Where you just loop through each line, and replace "1" at the beginning of the line with 80.

A short version using regex and list comprehension:
import re
with open('source.txt', 'r') as infile:
reformatted = [re.sub(r'^1', '80', line) for line in infile.readlines()]
with open('source.txt', 'w') as outfile:
[outfile.write(line) for line in reformatted]

The input file contains lines made up of [white]space delimited tokens. If the first token equals '1' change it to '80'.
This can be achieved as follows:
with open('foo.txt', 'r+') as foo:
lines = foo.readlines()
foo.seek(0)
for line in lines:
if (tokens := line.split())[0] == '1':
tokens[0] = '80'
print(*tokens, file=foo)
foo.truncate()
Note:
Use of truncate isn't really necessary in this case because the file size will not increase but is a failsafe pattern for this kind of read/rewrite process

Related

I want to replace words from a file by the line no using python i have a list of line no?

if I have a file like:
Flower
Magnet
5001
100
0
and I have a list containing line number, which I have to change.
list =[2,3]
How can I do this using python and the output I expect is:
Flower
Most
Most
100
0
Code that I've tried:
f = open("your_file.txt","r")
line = f.readlines()[2]
print(line)
if line=="5001":
print "yes"
else:
print "no"
but it is not able to match.
i want to overwrite the file which i am reading
You may simply loop through the list of indices that you have to replace in your file (my original answer needlessly looped through all lines in the file):
with open('test.txt') as f:
data = f.read().splitlines()
replace = {1,2}
for i in replace:
data[i] = 'Most'
print('\n'.join(data))
Output:
Flower
Most
Most
100
0
To overwrite the file you have opened with the replacements, you may use the following:
with open('test.txt', 'r+') as f:
data = f.read().splitlines()
replace = {1,2}
for i in replace:
data[i] = 'Most'
f.seek(0)
f.write('\n'.join(data))
f.truncate()
The reason that you're having this problem is that when you take a line from a file opened in python, you also get the newline character (\n) at the end. To solve this, you could use the string.strip() function, which will automatically remove these characters.
Eg.
f = open("your_file.txt","r")
line = f.readlines()
lineToCheck = line[2].strip()
if(lineToCheck == "5001"):
print("yes")
else:
print("no")

Python filtering non alphanumeric not working properly

I have a text file with random letters,numbers and characters in it. I have to remove the special characters and only end up with alphanumeric ones, while printing the process.
Text file is like this:
fkdjks97#!%&jd
28e8uw99...
and so on
For some reason it's printing:
Line read' ,,s.8,ymsw5w-86
'
' ,,s.8,ymsw5w-86
'->' <filter object at 0x0000020406BC8550> '
These should go on only 2 lines, instead of 4. Like this:
Line read' ,,s.8,ymsw5w-86'
' ,,s.8,ymsw5w-86' -> 's8ymsw5w86'
My attempt:
file1 = open(textfile1,"r")
while True:
line = file1.readline()
line2 = filter(str.isalnum,line)
print("Line read'", str(line), "'")
print("'", str(line), "'->'", line2, "'")
if len(line) == 0:
break
filter() is an iterator object; you'll need to actually iterate over it to pull out the results.
In this case, you want a string back, so you could use str.join() to do the iteration and put everything back into a single string:
line2 = ''.join(filter(str.isalnum, line))
Note that you shouldn't really need to use a while True loop with file1.readline() calls. You can use a for loop directly over the file to get the lines by replacing the while True, line = file1.readline() and if len(line) == 0: break lines with:
for line in file1:
# ...
You might be looking for a regex solution:
import re
rx = re.compile(r'[^A-Za-z]+')
# some sample line
line = 'fkdjks97#!%&jd'
# and then later on
line = rx.sub('', line)
print(line)
Which yields
# fkdjksjd
Putting this in a with... construct, you might be using
with open(textfile1, "r") as fp:
line = rx.sub('', fp.readline())
print(line)

how can i convert surname:name to name:surname? [duplicate]

In Python, calling e.g. temp = open(filename,'r').readlines() results in a list in which each element is a line from the file. However, these strings have a newline character at the end, which I don't want.
How can I get the data without the newlines?
You can read the whole file and split lines using str.splitlines:
temp = file.read().splitlines()
Or you can strip the newline by hand:
temp = [line[:-1] for line in file]
Note: this last solution only works if the file ends with a newline, otherwise the last line will lose a character.
This assumption is true in most cases (especially for files created by text editors, which often do add an ending newline anyway).
If you want to avoid this you can add a newline at the end of file:
with open(the_file, 'r+') as f:
f.seek(-1, 2) # go at the end of the file
if f.read(1) != '\n':
# add missing newline if not already present
f.write('\n')
f.flush()
f.seek(0)
lines = [line[:-1] for line in f]
Or a simpler alternative is to strip the newline instead:
[line.rstrip('\n') for line in file]
Or even, although pretty unreadable:
[line[:-(line[-1] == '\n') or len(line)+1] for line in file]
Which exploits the fact that the return value of or isn't a boolean, but the object that was evaluated true or false.
The readlines method is actually equivalent to:
def readlines(self):
lines = []
for line in iter(self.readline, ''):
lines.append(line)
return lines
# or equivalently
def readlines(self):
lines = []
while True:
line = self.readline()
if not line:
break
lines.append(line)
return lines
Since readline() keeps the newline also readlines() keeps it.
Note: for symmetry to readlines() the writelines() method does not add ending newlines, so f2.writelines(f.readlines()) produces an exact copy of f in f2.
temp = open(filename,'r').read().split('\n')
Reading file one row at the time. Removing unwanted chars from end of the string with str.rstrip(chars).
with open(filename, 'r') as fileobj:
for row in fileobj:
print(row.rstrip('\n'))
See also str.strip([chars]) and str.lstrip([chars]).
I think this is the best option.
temp = [line.strip() for line in file.readlines()]
temp = open(filename,'r').read().splitlines()
My preferred one-liner -- if you don't count from pathlib import Path :)
lines = Path(filename).read_text().splitlines()
This it auto-closes the file, no need for with open()...
Added in Python 3.5.
https://docs.python.org/3/library/pathlib.html#pathlib.Path.read_text
Try this:
u=open("url.txt","r")
url=u.read().replace('\n','')
print(url)
To get rid of trailing end-of-line (/n) characters and of empty list values (''), try:
f = open(path_sample, "r")
lines = [line.rstrip('\n') for line in f.readlines() if line.strip() != '']
You can read the file as a list easily using a list comprehension
with open("foo.txt", 'r') as f:
lst = [row.rstrip('\n') for row in f]
my_file = open("first_file.txt", "r")
for line in my_file.readlines():
if line[-1:] == "\n":
print(line[:-1])
else:
print(line)
my_file.close()
This script here will take lines from file and save every line without newline with ,0 at the end in file2.
file = open("temp.txt", "+r")
file2 = open("res.txt", "+w")
for line in file:
file2.writelines(f"{line.splitlines()[0]},0\n")
file2.close()
if you looked at line, this value is data\n, so we put splitlines()
to make it as an array and [0] to choose the only word data
import csv
with open(filename) as f:
csvreader = csv.reader(f)
for line in csvreader:
print(line[0])

Delete the first line after the specific word (COMPDAT) only if this first line contains "1 1" with Python

I asked the similar question How to delete one line after the specific word with Python . However, I want to add one more condition. So I want to have two conditions to delete the first line:
After the word "COMPDAT".
Only if this first line contains "1" for item 4 and 5.
For example:
COMPDAT
'9850' 125 57 1 1 OPEN /
The code suggested in my previous question works only for condition 1:
input_file = open("input.txt", 'r')
prev_line = False
lines =[]
for line in input_file:
if not prev_line:
lines.append(line)
prev_line=False
if "COMPDAT" in line:
prev_line=True
input_file.close()
input_file = open("input.txt", 'w')
for line in lines:
input_file.write(line)
input_file.close()
How to change this code in order to satisfy also the second condition?
Thank you!
This is based on my answer to your other question
def line_and_line_before(file):
prev_line = None
for line in file:
yield (prev_line, line)
prev_line = line
def has_ones(line):
splitted_line = line.split(" ")
return len(splitted_line) > 4 and splitted_line[3] == '1' and splitted_line[4] == '1'
input_file = open("input.txt", 'r')
lines = []
for prev_line, line in line_and_line_before(input_file):
if (not prev_line or "COMPDAT" not in prev_line) or not has_ones(line):
lines.append(line)
input_file.close()
input_file = open("input.txt", 'w')
for line in lines:
input_file.write(line)
input_file.close()
You need to think in terms of when to keep line instead of when to remove line.

How to completely remove "\n" in text file using python

So the text file I have is formatted something like this:
a
b
c
I know how to strip() and rstrip() but I want to get rid of the empty lines.
I want to make it shorter like this:
a
b
c
You could remove all blank lines (lines that contain only whitespace) from stdin and/or files given at the command line using fileinput module:
#!/usr/bin/env python
import sys
import fileinput
for line in fileinput.input(inplace=True):
if line.strip(): # preserve non-blank lines
sys.stdout.write(line)
You can use regular expressions :
import re
txt = """a
b
c"""
print re.sub(r'\n+', '\n', txt) # replace one or more consecutive \n by a single one
However, lines with spaces won't be removed. A better solution is :
re.sub(r'(\n[ \t]*)+', '\n', txt)
This way, wou will also remove leading spaces.
Simply remove any line that only equals "\n":
in_filename = 'in_example.txt'
out_filename = 'out_example.txt'
with open(in_filename) as infile, open(out_filename, "w") as outfile:
for line in infile.readlines():
if line != "\n":
outfile.write(line)
If you want to simply update the same file, close and reopen it to overwrite it with the new data:
filename = 'in_example.txt'
filedata = ""
with open(filename, "r") as infile:
for line in infile.readlines():
if line != "\n":
filedata += line
with open(filename, "w") as outfile:
outfile.write(filedata)

Categories