replace a string within file using python - python-3.x

I am getting an odd problem. when I am trying to replace a string in a file.
The relevant line in the file is:
lattice parameter A [a.u.]
5.771452243459
and I am trying to replace it as:
with open(newsys, "r+") as finp:
for line in finp:
# print(line)
if line.startswith("lattice parameter A [a.u.]"):
line = next(finp)
print(line)
print(Alat)
line.replace(line.strip(), str(Alat))
print(line)
the last 3 print statement gives:
5.771452243459 # string that will be replaced
6.63717007997785 #value of Alat
5.771452243459 #the line after replace statement
What is going wrong here?

replace method does not modify existing string. Instead it is creating a new one. So in line
line.replace(line.strip(), str(Alat))
You are creating a completely new string and discards it (because not assign to any variable).
I would do something like:
with open(newsys, "r+") as finp:
with open('newfile', 'w') as fout:
for line in finp:
# print(line)
if line.startswith("lattice parameter A [a.u.]"):
line = next(finp)
print(line)
print(Alat)
line = line.replace(line.strip(), str(Alat))
print(line)
fout.write(line)

Related

Python: Reading line with 'readline()' function and appending to a list

My code:
In my file i have these numbers in a list
charge_account = ['4654145', '9658115', '5658845', '5658045', '6181531', '2134874', '5964554']
I am reading the file with a function, appending it to a list and then returning the list:
import os
os.system('cls')
def fileReader():
contentList = []
with open('charge_accounts.txt','r') as f:
line = f.readline().rstrip('\n')
while line !="":
line = f.readline().rstrip(' \n')
contentList.append(line)
# print(contentList)
# print(len(contentList))
#contentList = contentList[:-1]
print(contentList)
return contentList
Now my question is, when i read all the file content and append them to my list, i am getting an extra blank string at the end of the list.
output:
['4654145', '9658115', '5658845', '5658045', '6181531', '2134874', '5964554', '']
Now i have solved it by using slicing (as i commented them out) but i still have not figured out why i am getting the ' ' in the end of the list. i tried filtering it out but noting happens. i have checked if it there is an extra line in the end of the file but what am i doing wrong ?
There are a couple of things. You are reading the file line by line in the while loop. This means that after the last line is read, the while condition is still true so you read an extra line (which is empty) but still added to your list.
But you don't need a while loop: use lines = f.readlines(). It will read the whole file in a list, and you almost have the list you are aiming for. Almost, because you need to strip each element:
def fileReader():
with open('charge_accounts.txt','r') as f:
lines = f.readlines()
return [line.strip() for line in lines]
print(fileReader())
while line !="":
contentList.append(line)
line = f.readline().rstrip(' \n')
print(contentList)
I realized i had to append the while loop primer into the list which i read before the loop started. content.append(line) had to be the first statement in the while loop. This solves the blank entry in the end of list, which in hindsight i realize means that i skipped the first readline value.

how to print lines in a txt which fulfills certain condition with python?

def palindrome_filter(file_name):
file=open('text_1.txt','r')
for line in file:
line=line.rstrip()
if (is_palindrome(line)):
print(line)
file.close()
palindrome_filter('text_1.txt')
Above are the codes that I wrote. I actually want python to print the certain line which is a palindrome, but it keeps printing true or false instead. I wonder what's wrong with my code. (function to determine whether a line is palindrome is texted, it is correct.)
Your solution might be:
def palindrome_filter(file_name):
file=open('text_1.txt','r')
for line in file:
line = line.rstrip()
if line == line[::-1]:
print(line)
file.close()

How can I start reading text at a specific line and stop and specific line

Long time listener first time caller, I'm quite new to this so please be kind.
I have a large text document and I would like to strip out the headers and footers. I would like to trigger the start and stop reading lines with specific strings in the text.
filename ='Bigtextdoc.txt'
startlookup = 'Foo'
endlookup = 'Bar'
with open(filename, 'r') as infile:
for startnum, line in enumerate(infile, 1):
if startlookup in line:
data = infile.readlines()
for endnum, line in enumerate(infile, 1):
if endlookup in line:
break
print(data)
like this I can read the lines after the header contain 'Foo' and If I move the data = line after the if endlookup line it will only read the line in the footer starting at 'Bar'
I don't know how to start at Foo and stop at Bar?
For readability I'll extract the logic in a function like:
def lookup_between_tags(lines, starttag, endtag):
should_yield = False
for line in lines:
if starttag in line:
should_yield = True
elif endtag in line:
should_yield = False
if should_yield:
yield line
Using the fact that an opened file is iterable, it can be used like:
with open('Bigtextdoc.txt') as bigtextdoc:
for line in lookup_between_tags(bigtextdoc, 'Foo', 'Bar'):
print(line)

How to print a file containing a list

So basically i have a list in a file and i only want to print the line containing an A
Here is a small part of the list
E5341,21/09/2015,C102,440,E,0
E5342,21/09/2015,C103,290,A,290
E5343,21/09/2015,C104,730,N,0
E5344,22/09/2015,C105,180,A,180
E5345,22/09/2015,C106,815,A,400
So i only want to print the line containing A
Sorry im still new at python,
i gave a try using one "print" to print the whole line but ended up failing guess i will always suck at python
You just have to:
open file
read lines
for each line, split at ","
for each line, if the 5th part of the splitted str is equal to "A", print line
Code:
filepath = 'file.txt'
with open(filepath, 'r') as f:
lines = f.readlines()
for line in lines:
if line.split(',')[4] == "A":
print(line)

How to remove '#' comments from a string?

The problem:
Implement a Python function called stripComments(code) where code is a parameter that takes a string containing the Python code. The function stripComments() returns the code with all comments removed.
I have:
def stripComments(code):
code = str(code)
for line in code:
comments = [word[1:] for word in code.split() if word[0] == '#']
del(comments)
stripComments(code)
I'm not sure how to specifically tell python to search through each line of the string and when it finds a hashtag, to delete the rest of the line.
Please help. :(
You could achieve this through re.sub function.
import re
def stripComments(code):
code = str(code)
return re.sub(r'(?m)^ *#.*\n?', '', code)
print(stripComments("""#foo bar
bar foo
# buz"""))
(?m) enables the multiline mode. ^ asserts that we are at the start. <space>*# matches the character # at the start with or without preceding spaces. .* matches all the following characters except line breaks. Replacing those matched characters with empty string will give you the string with comment lines deleted.
def remove_comments(filename1, filename2):
""" Remove all comments beginning with # from filename1 and writes
the result to filename2
"""
with open(filename1, 'r') as f:
lines = f.readlines()
with open(filename2, 'w') as f:
for line in lines:
# Keep the Shebang line
if line[0:2] == "#!":
f.writelines(line)
# Also keep existing empty lines
elif not line.strip():
f.writelines(line)
# But remove comments from other lines
else:
line = line.split('#')
stripped_string = line[0].rstrip()
# Write the line only if the comment was after the code.
# Discard lines that only contain comments.
if stripped_string:
f.writelines(stripped_string)
f.writelines('\n')
For my future reference.
def remove_comments(lines: list[str]) -> list[str]:
new_lines = []
for line in lines:
if line.startswith("#"): # Deal with comment as the first character
continue
line = line.split(" #")[0]
if line.strip() != "":
new_lines.append(line)
return new_lines
print(remove_comments("Hello #World!\n\nI have a question # that #".split('\n')))
>>> ['Hello', 'I have a question']
This implementation has benefit of not requiring the re module and being easy to understand. It also removes pre-existing blank lines, which is useful for my use case.

Resources