Clean the csv data using python - 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

Related

Remove all blank lines from a text file in 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()])

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

I need to search a string in data file. but the string is written in another file

I have key words to be search in one file let say abc.txt and in another file I have my data, def.txt.
I want a code in python to find key words written in abc.txt, in def.txt and if present, print those line in a new file.
Thank you.
I tried writing a code but it didn't work.
following is the code I write.
f = open('/home/vivek/Documents/abc.txt')
f1 = open('output.txt', 'a')
f2 = open('/home/vivek/Documents/def.txt', 'r')
# doIHaveToCopyTheLine=False
for line in f.readlines():
if f2 in line:
f1.write(line)
f1.close()
f.close()
f2.close()
Load the keywords into a list then you can check the other file line-by-line, and write to outfile as you find keywords in the line.
with open('/path/to/keywords.txt') as f:
keywords = set(line.strip() for line in f) # assuming words are separated by line
with open('/path/to/search_me.txt') as f, open('/path/to/outfile.txt', 'w') as outfile:
for line in f:
if any(kw in line for kw in keywords):
outfile.write(line)
You should record all the words in abc.txt use a set and then search them in def.txt
word_set = set()
with open('/home/vivek/Documents/abc.txt') as f:
for line in f:
word_set.add(line.strip())
f1 = open('output.txt', 'a')
with open('/home/vivek/Documents/def.txt') as f:
for line in f:
find = False
for word in word_set:
if word in line:
find = True
break
if find:
f1.write(line)
f1.close()
You can try this code:
with open("keyword.txt", "r") as keyword_file:
keywords = keyword_file.read().strip()
keywords = keywords.split()
with open("data.txt", "r") as data_file, open("output.txt", "w") as output_file:
for line in data_file.readlines():
line = line.strip()
for word in keywords:
if line.find(word) != -1:
print line
output_file.writelines(line + '\n')
break
In addition to sytech's answer you may try this:
with open('def.txt') as kw_obj, open('abc.txt') as in_obj:
keywords = set(kw_obj.read().split())
in_lines = in_obj.readlines()
match_lines = [line for keyword in keywords for line in in_lines if keyword in line]
if match_lines:
with open('out.txt', 'w') as out:
out.write(''.join(match_lines))

isolate and work with certain lines in text file

I'm using python. I've got a text file that has the following format:
###########
text lines
###########
text lines
###########
text lines
###########
I want to run an algorithm for each segment found between two "######" lines.
How do I reference the text lines between two "######" lines.
The number of lines between two "######" lines is not fixed.
thanks
You can do this quite easily with split():
with open('myfile.txt') as f:
data = f.read().split('###########')
print([txt.strip() for txt in data if txt])
Would this work?
f = open("textfile.txt")
for line in f.readlines():
if '######' not in line:
print(line) # evaluate and process the line here
Or how about this:
with open("f.txt") as f:
print(''.join(x for x in filter(lambda x: x != '###########', f.read().split("\n"))))
file.txt:
###########
line1
#
line2
######
line3
line4
line5
##########
line6
line7
code:
import re
FILE_PATH = "file.txt"
blocks = []
lines = []
with open(FILE_PATH) as file:
for line in file:
if re.fullmatch(r"#+(\n){0,1}", line):
if lines:
blocks.append(lines)
lines = []
else:
lines.append(line.rstrip("\n")) # remove rstrip if you want to keep a new line at the end of a line
# store last block (if file does not end with #+ line)
if lines:
blocks.append(lines)
print(blocks) # [['line1'], ['line2'], ['line3', 'line4', 'line5'], ['line6', 'line7']]
Considering "######" doesn't contain "\n":
def get_chunks(filename):
with open(filename) as f:
return f.read().split('#'*6)[2:-1:2]
else:
import re
def get_chunks(filename):
with open(filename) as f:
return re.split(r'(#\n?){6}', f.read())[4:-1:4]

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