skip the first line after if condition - python

When I read a file line by line, I want to skip the first line that starts with ; after if condition[ word ] but my code gives the error below. How can I fix this error?
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'
Code:
for line in open(inputfile, "r").readlines():
if "[ word ]" in line:
line = open(inputfile, "r").next()

You can use startswith to check how the line begins, and continue to move past that line
with open(inputfile, "r") as f:
for line in f:
if line.startswith(';'):
continue
...

You can do it like so:
with open(inputfile) as f:
for line in f:
if "[ word ]" in line:
line = f.readline() # 1
reassigns line to the next line than the one used by the if comparison.

you should just use continue and add variable to check is first line skiped:
with open(inputfile, "r") as f:
skip_first = False
for line in f:
if line.startswith(';') and skip_first:
skip_first = True
continue

Related

Remove a word from a file

I am try to sort through the following file
Fantasy
Supernatural
Fantasy
UrbanFantasy
Fantasy
EpicFantasy
Fantasy
HighFantasy
I want to remove the word fantasy when it appears by itself and put the new list into another file
I tried
def getRidofFantasy():
file = open("FantasyGenres.txt", "r")
new_file = open("genres/fantasy", "w")
for line in file:
if line != "Fantasy":
new_file.write(line)
file.close()
new_file.close()
This does not work and I am at a lost as to why. The new file is the same as the old one. Can anyone explain what's happening and give an example of the correct solution?
Try this
with open('fantasy.txt') as f, open('generes/fantasy', 'w') as nf:
lines = [line+'\n' for line in f.read().splitlines() if line != "Fantasy"]
nf.writelines(lines)
In your code when you do for line in f the line variable also include the \n (endline) char, that's why it doesn't work.
Try this. -
def getRidofFantasy():
with open("FantasyGenres.txt", "r") as file:
content = [line.strip('\n') for line in file.readlines()]
new_list = list(filter(lambda a: a != 'Fantasy', content))
with open("genres/fantasy.txt", "w") as new_file:
[new_file.write(f'{line}\n') for line in new_list]
getRidofFantasy()
Similar to #Atin's answer, you can also do this:
with open('fantasy.txt') as f, open('generes/fantasy', 'w') as nf:
lines = [line for line in f.readlines() if line.strip() != "Fantasy"]
nf.writelines(lines)
That is because a new line is also a character:
Fantasy\n
Supernatural\n
etc.
You have to account for that. One possibility:
def getRidofFantasy():
with open("FantasyGenres.txt", "r") as f: # this way Python closes the file buffer for you
oldfile = f.readlines()
new_file = open("genres/fantasy", "w")
for line in oldfile:
line = line.rstrip('\n')
if line != "Fantasy":
new_file.write(line+'\n') # make sure to append the newline character again
new_file.close()
Okay so there is one thing you should know. When you read a line like that the variable will look something like this:-
line='Fantasy\n'
So, you need to strip that character. The simple solution without changing any of your code would be to just change the if statement. Change it to
if not 'Fantasy'== line.strip() and keep your code as it is and the new file that'll be generated will be the one you want.

Open a JS file and edit a line with Python

I'm trying to modify a specific line in a js file using python.
Here's the js file :
...
hide: [""]
...
Here's my python code :
with open('./config.js','r') as f:
lines = f.readlines()
with open('./config.js','w') as f:
for line in lines:
line = line.replace('hide', 'something')
f.write(line)
So it works but this is not what I want to do.
I want to write 'something' between the brackets and not replace 'hide'.
So I don't know how to do it: Do I have to replace the whole line or can I just add a word between the brackets?
Thanks
If you want to replace text at this exact line you could just do:
with open('./config.js','r') as f:
lines = f.readlines()
with open('./config.js','w') as f:
  new_value = 'Something New'
for line in lines:
if line.startswith('hide'):
line = 'hide: ["{}"]'.format(new_value)
f.write(line)
or alternatively in the conditional
if line.startswith('hide'):
line = line.replace('""', '"Something new"')
Here's way to replace any value in brackets for hide that starts with any spacing.
lines = '''\
first line
hide: [""]
hide: ["something"]
last line\
'''
new_value = 'new value'
for line in lines.splitlines():
if line.strip().startswith('hide'):
line = line[:line.index('[')+2] + new_value + line[line.index(']')-1:]
print(line)
Output:
first line
hide: ["new value"]
hide: ["new value"]
last line
You can use fileinput and replace it inplace:
import fileinput
import sys
def replaceAll(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
replaceAll("config.js",'hide: [""]','hide: ["something"]')
Reference
If hide: [""] is not ambiguous, you could simply load the whole file, replace and write it back:
newline = 'Something new'
with open('./config.js','r') as f:
txt = f.read()
txt = txt.replace('hide: [""]', 'hide: ["' + newline + '"]')
with open('./config.js','w') as f:
f.write(txt)
As long as you don't have "hide" anywhere else in the file, then you could just do
with open('/config.js','r') as f:
lines = f.readlines()
with open('./config.js','w') as f:
for line in lines:
line = line.replace('hide [""]', 'hide ["something"]')
f.write(line)
You can do this using re.sub()
import re
with open('./config.js','r') as f:
lines = f.readlines()
with open('./config.js','w') as f:
for line in lines:
line = re.sub(r'(\[")("\])', r'\1' + 'something' + r'\2', line)
f.write(line)
It works by searching for a regular expression, but forms a group out of what you want on the left ((\[")) and the right (("\])). You then concatenate these either side of the text you want to insert (in this example 'something').
The bounding ( ) makes a group which can be accessed in the replace with r'\1', then second group is r'\2'.

List append and wrtite to an output

I am trying to append a string to my list and then write to an file
I used the following code:
temp=[line.split() for line in file]
for line in temp:
line=line.split()
line.append("string")
line=" ".join(line[0:-1]))
outputfile.write(line+'\n')
But when I checked the output file, instead of giving me string at the last field, it outputs some random number. Does anyone know what it happened?
Perhaps this is what you meant, it appends "string" to the end of each line.
outputfile = open( 'temp2.txt', 'w' )
temp=[ line.split() for line in open( 'temp1.txt' )]
for line in temp:
line.append("string")
line=" ".join(line)
outputfile.write(line+'\n')
Here is another way to do it:
outputfile = open( 'temp2.txt', 'w' )
temp=[ line.strip() for line in open( 'temp.txt' )]
for line in temp:
line = line + " string"
outputfile.write(line+'\n')
fname = "test.txt"
with open(fname,'r+') as f:
lines = f.readlines()
f.seek(0)
f.truncate()
for line in lines:
listString = line.split()
listString.append("your string \n")
newLine = " ".join(listString)
f.write(newLine)
test.txt
test
this is a file u read from
and u can add string at the end of each line
output test.txt
test your string
this is a file u read from your string
and u can add string at the end of each line your string
just make sure to add "\n" at the end of ur line for the new line

Skip a line when using with..open

I am reading a large text file line by line
import re
with open("file.txt") as f:
for line in f:
if re.search(regex stuff, line):
#skip next line
I would like to skip a line if I find a match - how can I do this using with...open statement?
You condition should be if not search(regex stuff,line)
import re
with open("file.txt") as f:
for line in f:
if not re.search(regex stuff, line):
continue # or do something
Use next on the original file-like obj f.
import re
with open("file.txt") as f:
for line in f:
if re.search(regex, line):
next_line = next(f, '')
This will put the contents of the following line in next_line or if you matched on the last line of the file, it'll be an empty string. Do something with next_line if you want. When the for-loop resumes it will be from the line following that one...
you'll need some state. I'll call that state skipnext:
import re
skipnext = False
with open("file.txt") as f:
if skipnext:
skipnext = False
continue
for line in f:
if re.search(regex stuff, line):
skipnext = True

Match the last word and delete the entire line

Input.txt File
12626232 : Bookmarks
1321121:
126262
Here 126262: can be anything text or digit, so basically will search for last word is : (colon) and delete the entire line
Output.txt File
12626232 : Bookmarks
My Code:
def function_example():
fn = 'input.txt'
f = open(fn)
output = []
for line in f:
if not ":" in line:
output.append(line)
f.close()
f = open(fn, 'w')
f.writelines(output)
f.close()
Problem: When I match with : it remove the entire line, but I just want to check if it is exist in the end of line and if it is end of the line then only remove the entire line.
Any suggestion will be appreciated. Thanks.
I saw as following but not sure how to use it in here
a = "abc here we go:"
print a[:-1]
I believe with this you should be able to achieve what you want.
with open(fname) as f:
lines = f.readlines()
for line in lines:
if not line.strip().endswith(':'):
print line
Here fname is the variable pointing to the file location.
You were almost there with your function. You were checking if : appears anywhere in the line, when you need to check if the line ends with it:
def function_example():
fn = 'input.txt'
f = open(fn)
output = []
for line in f:
if not line.strip().endswith(":"): # This is what you were missing
output.append(line)
f.close()
f = open(fn, 'w')
f.writelines(output)
f.close()
You could have also done if not line.strip()[:-1] == ':':, but endswith() is better suited for your use case.
Here is a compact way to do what you are doing above:
def function_example(infile, outfile, limiter=':'):
''' Filters all lines in :infile: that end in :limiter:
and writes the remaining lines to :outfile: '''
with open(infile) as in, open(outfile,'w') as out:
for line in in:
if not line.strip().endswith(limiter):
out.write(line)
The with statement creates a context and automatically closes files when the block ends.
To search if the last letter is : Do following
if line.strip().endswith(':'):
...Do Something...
You can use a regular expression
import re
#Something end with ':'
regex = re.compile('.(:+)')
new_lines = []
file_name = "path_to_file"
with open(file_name) as _file:
lines = _file.readlines()
new_lines = [line for line in lines if regex.search(line.strip())]
with open(file_name, "w") as _file:
_file.writelines(new_lines)

Categories

Resources