Just a basic question. I know how to read information from a file etc but how would I go about only including the lines that are in between certain lines?
Say I have this :
Information Included in file but before "beginning of text"
" Beginning of text "
information I want
" end of text "
Information included in file but after the "end of text"
Thank you for any help you can give to get me started.
You can read the file in line by line until you reach the start-markerline, then do something with the lines (print them, store them in a list, etc) until you reach the end-markerline.
with open('myfile.txt') as f:
line = f.readline()
while line != ' Beginning of text \n':
line = f.readline()
while line != ' end of text \n':
# add code to do something with the line here
line = f.readline()
Make sure to exactly match the start- and end-markerlines. In your example they have a leading and trailing blank.
Yet another way to do it, is to use two-argument version of iter():
start = '" Beginning of text "\n'
end = '" end of text "\n'
with open('myfile.txt') as f:
for line in iter(f.readline, start):
pass
for line in iter(f.readline, end):
print line
see https://docs.python.org/2/library/functions.html#iter for details
I would just read the file line by line and check each line if it matches beginning or end string. The boolean readData then indicates if you are between beginning and end and you can read the actual information to another variable.
# Open the file
f = open('myTextFile.txt')
# Read the first line
line = f.readline()
readData=false;
# If the file is not empty keep reading line one at a time
# until the file is empty
while line:
# Check if line matches beginning
if line == "Beginning of text":
readData=true;
# Check if line matches end
if line == "end of text"
readData=false;
# We are between beginning and end
if readData:
(...)
line = f.readline()
f.close()
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 searched this on Stackoverflow and all of the "duplicates" for this topic but it seems remained unanswered. I have tried all these:
Attempt#1:
for word in header:
writer.writerow([word]
Pasted from writing data from a python list to csv row-wise
Attempt#2:
And this one, should have been close but it has a bug:
# Open a file in witre mode
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
Pasted from <http://www.tutorialspoint.com/python/file_writelines.htm>
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
seq = ["This is 6th line\n", "This is 7th line"]
# Write sequence of lines at the end of the file.
fo.seek(0, 2)
line = fo.writelines( seq )
# Now read complete file from beginning.
fo.seek(0,0)
for index in range(7):
line = fo.next()
print "Line No %d - %s" % (index, line)
# Close opend file
fo.close()
Pasted from http://www.tutorialspoint.com/python/file_writelines.htm
Attempt#3:
>>>outF = open("myOutFile.txt", "w")
>>>for line in textList:
... outF.write(line)
... outF.write("\n")
>>>outF.close()
Pasted from http://cmdlinetips.com/2012/09/three-ways-to-write-text-to-a-file-in-python/
Attempt#4:
with open('file_to_write', 'w') as f:
f.write('file contents')
Pasted from Correct way to write line to file in Python
Attempt#5:
This one which uses the append when writing to file.. but it appends each line at the end of each row. So it would be hard for me to separate all the rows.
append_text = str(alldates)
with open('my_file.txt', 'a') as lead:
lead.write(append_text)
Pasted from Python: Saving a string to file without overwriting file's contents
Can someone help me how to write a newline of row per iteration in a loop to a file without overwriting the file?
data = [1,2,3,4,5]
with open('asd.txt', 'w') as fn:
for i in data:
fn.write(str(i) + '\n') # Add a \n (newline) so the next write will occure in the next line
Content of asd.txt:
1
2
3
4
5
If you want to append to a file use with open('asd.txt', 'a') as fn:
There is 2 ways to do that:
First by adding '\n' character at the end of your output line :
for x in row:
writer.write(x + "\n")
Second by opening file in Append mode, it is going to add lines existing text file but be careful.it's not overwrite the file :
fw = open(myfile.txt,'a')
I have been doing python tasks for learning and I came across this task where I have to read a file that includes few words and if a line is palindrome (same when written backwards: lol > lol)
so I tried with this code but It doesn't print anything on the terminal:
with open("words.txt") as f:
for line in f:
if line == line[::-1]:
print line
But if I print like this, without an if condition, it prints the words:
with open("words.txt") as f:
for line in f:
print line
I wonder why It wont print the words that I've written in the file:
sefes
kurwa
rawuk
lol
bollob
This is because those lines contain "\n" on the end. "\n" means new line. Therefore none of those are palindromes according to python.
You can strip off the "\n" first by doing:
with open("words.txt") as f:
for line in f:
if line.strip() == line.strip()[::-1]:
print line
The last character of each line is a newline character ("\n"). You need to strip trailing newlines ("foo\n".strip()) before checking whether the line is a palindrome.
When you read a line from a file like this, you also get the newline character. So, e.g., you're seeing 'sefes\n', which when reversed is '\nsefes'. These two lines are of course not equal. One way to solve this is to use rstrip to remove these newlines:
with open("words.txt") as f:
for line in f:
line = line.rstrip()
if line == line[::-1]:
print line
I would like to unescape unicode characters in the source file:
source = open('source.csv', 'r')
target = open('target.csv', 'w')
target.write(source.read().decode('unicode_escape').encode('utf-8'))
But the result file contains extra line breaks. For example, the text
u'\u0417a\u0439\u043c\u044b \u0412ce\u043c \u0436e\u043ba\u044e\u0449\u0438\u043c!\nO\u0434o\u0431\u0440e\u043d\u0438e 98%'
is replaced with
u'Зaймы Вceм жeлaющим!
Oдoбрeниe 98%'
Understand that there is line break symbol \n in the source text, but I would like to keep it as is without actual conversion to line break.
You're almost there:
for line in source:
line = line.rstrip('\n')
line = line.decode('unicode_escape').replace(u'\n', u'\\n').encode('utf8')
target.write(line + '\n')
Hi I already have the search function sorted out:
def searchconfig():
config1 = open("config.php", "r")
b='//cats'
for num, line in enumerate(config1,0):
if b in line:
connum = num + 1
return connum
config1.close()
This will return the line number of //cats, I then need to take the data underneath it put it in a tempoary document, append new data under the //cats and then append the data in the tempoary document to the original? how would i do this? i know that i would have to use 'a' instead of 'r' when opening the document but i do not know how to utilise the line number.
I think, the easiest way would be to read the whole file into a list of strings, work on that list and write it back afterwards.
# Read all lines of the file into a list of strings
with open("config.php", "r") as file:
lines = list(file)
file.close()
# This gets the line number for the first line containing '//cats'
# Note that it will throw an StopIteration exception, if no such line exists...
linenum = (num for (num, line) in enumerate(lines) if '//cats' in line).next()
# insert a line after the line containing '//cats'
lines.insert(linenum+1, 'This is a new line...')
# You could also replace the line following '//cats' like
lines[linenum+1] = 'New line content...'
# Write back the file (in fact this creates a new file with new content)
# Note that you need to append the line delimiter '\n' to every line explicitely
with open("config.php", "w") as file:
file.writelines(line + '\n' for line in lines)
file.close()
Using "a" as mode for open would only let you append ath the end of the file.
You could use "r+" for a combined read/write mode, but then you could only overwrite some parts of the file, there is no simple way to insert new lines in the middle of the file using this mode.
You could do it like this. I am creating a new file in this example as it is usually safer.
with open('my_file.php') as my_php_file:
add_new_content = ['%sNEWCONTENT' %line if '//cat' in line
else line.strip('\n')
for line in my_php_file.readlines()]
with open('my_new_file.php', 'w+') as my_new_php_file:
for line in add_new_content:
print>>my_new_php_file, line