Remove all blank lines from a text file in python - python

I'm trying to figure out how to remove an empty line from a text file while only using python.
The input should be like:
firstline
secondline
thirdline
And the output should be:
firstline
secondline
thirdline
So I have this right now...
import sys
with open("New Text Document.txt") as f:
for line in f:
if not line.isspace():
sys.stdout.write(line)

file = open('demo.txt', 'r')
arr = file.readlines()
print([v for v in arr if not v.isspace()])

Related

Fetching a line which comes 2 lines after searched line from a text file in python

Text file contains below data:
InitialSearch='Searched data'
file = open("textfile.txt","r")
lines = file.readlines()
file.close()
fileOutput = open ('NewTextFile.txt', 'w')
for x,line in enumerate(lines):
if line.find(InitialSearch)>=0:
fileOutput.write(line)
fileOutput.close
Code is not properly working
You already have the index of the "matched" line in your for loop. Just add two to it, and you will have the row you want to add to the output file.
InitialSearch='Searched data'
file = open("textfile.txt","r")
lines = file.readlines()
file.close()
with open('NewTextFile.txt', 'w') as fileOutput
for x,line in enumerate(lines):
if line.find(InitialSearch)>=0:
fileOutput.write(lines[x+2])

Converting multi line txt file into CSV using python

Hello I'm trying to convert some .txt files into csv files using python but I can't seem to manage it. Basically everything is just in the one column, rather separate and within the one row.
.txt file
Heading test
Stuff, Stuffing, Stuffer
https://www.test.com/testSearch/test.html
Python
import csv
import re
with open('string.txt', 'r') as in_file:
stripped = (line.strip() for line in in_file)
lines = (re.split(';|,|\n',line) for line in stripped if line)
with open('log.csv', 'w') as out_file:
writer = csv.writer(out_file)
writer.writerow(('title','topic 1', 'topic 2' ,'topic 3','link'))
writer.writerows(lines)
Output
title,topic 1,topic 2,topic 3,link
Heading test
Stuff, Stuffing, Stuffer
https://www.test.com/testSearch/test.html
try the below code:
import csv
with open('sample.txt', 'r') as in_file:
stripped = [line.strip() for line in in_file]
lines = [line.split(",") for line in stripped if line]
lines1 = [li for line in lines for li in line]
with open('log.csv', 'wb') as out_file:
writer = csv.writer(out_file)
writer.writerow(('title', 'intro'))
writer.writerow(lines1)

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'.

Append String to each line of .txt file in python?

I want to append some text to every line in my file
Here is my code
filepath = 'hole.txt'
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
#..........
#want to append text "#" in every line by reading line by line
text from .txt file
line = fp.readline()
cnt += 1
You can read the lines and put them in a list. Then you open the same file with write mode and write each line with the string you want to append.
filepath = "hole.txt"
with open(filepath) as fp:
lines = fp.read().splitlines()
with open(filepath, "w") as fp:
for line in lines:
print(line + "#", file=fp)
Assuming you can load the full text in memory, you could open the file, split by row and for each row append the '#'. Then save :-) :
with open(filepath, 'r') as f: # load file
lines = f.read().splitlines() # read lines
with open('new_file.txt', 'w') as f:
f.write('\n'.join([line + '#' for line in lines])) # write lines with '#' appended
I'll assume the file is small enough to keep two copies of it in memory:
filepath = 'hole.txt'
with open(filepath, 'r') as f:
original_lines = f.readlines()
new_lines = [line.strip() + "#\n" for line in original_lines]
with open(filepath, 'w') as f:
f.writelines(new_lines)
First, we open the file and read all lines into a list. Then, a new list is generated by strip()ing the line terminators from each line, adding some additional text and a new line terminator after it.
Then, the last line overwrites the file with the new, modified lines.
does this help?
inputFile = "path-to-input-file/a.txt"
outputFile = "path-to-output-file/b.txt"
stringToAPpend = "#"
with open(inputFile, 'r') as inFile, open(outputFile, 'w') as outFile:
for line in inFile:
outFile.write(stringToAPpend+line)

Clean the csv data using python

I tried to remove the unwanted characters / # http form each line
codes below:
import csv
with open('C:\\project\\in.csv','r') as input_file:
with open('C:\\project\\out.csv','w') as output_file:
for L in input_file:
if L.endswith("/"):
newL=L.replace("/","")
output_file.write(newL)
elif L.find("#"):
newL,sep,tail=L.partition("#")
output_file.write(newL)
elif L.startswith('http:'):
newL=L.replace('http:','https:')
output_file.write(newL)
here is the mini example in.csv file for testing:
line1/
line2#sdgsgs
https://line3
http://line4
line5/
after make clean, I want it to be like :
line1
line2
https://line3
https://line4
line5
But the result not what I want, Can some one give me a hand.
Many Thanks, Henry
In this version a line can contain all of the replacement chars:
#!/usr/bin/env python
import csv
Output = []
with open('C:\\project\\in.csv', 'r') as input_file:
for line in input_file:
line = line.strip()
if line.endswith("/"):
line = line.replace("/", "")
if "#" in line:
line, sep, tail = line.partition("#")
if line.startswith('http:'):
line = line.replace('http:', 'https:')
Output.append(line)
with open('C:\\project\\out.csv', 'w') as output_file:
for output in Output:
output_file.write("{}\n".format(output))
Will output:
line1
line2
https://line3
https://line4
line5

Categories

Resources