Locked. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
Trying to write a program that takes in, from the command line, my executable file + an optional argument called 'no-comments' and the file.
So if someone writes in command line: stripWhiteSpace.py file.rtf
Then it will strip all the whitespace EXCEPT new lines.
If someone writes in command line: stripWhiteSpace.py no-comments file.rtf
Then it will strip all the whitespace except new lines, AND also remove all C/C++/Java style comments starting with "//" and anything that comes after that (that whole comment).
Here is my code (called stripWhiteSpace.py):
import sys
file = sys.argv[-1]
with open(file, 'r+') as f:
final_file = ""
if sys.argv[1] == "no-comments":
for line in f:
line = line.partition('//')[0]
line = line.strip(' \t\r')
final_file += line
else:
for line in f:
line = line.strip(' \t\r')
final_file += line
f.write(final_file)
The file is successfully passed through my python file. The problem is, it doesn't change. Any help is appreciated.
The problem is how you are using strip(). Doing line.strip(' \t\r') will strip only ' \t\r' ie a space, followed by a tab, followed by a carriage return. I dont think that is going to happen very often. The syntax should be line.strip([' ', '\t', '\r']).
Having said all that, if you are wanting to remove ALL white space, strip wont do that, it will only remove the white space at the start or end of the line. If you want to remove ALL the whitespace you need to use .replace().
Related
When you readline() from a file and try to write it to another txt file with text behind it the text behind it always goes to the next line, is there any way to not let it go to the next line and just put it behind it?
example code:
file = open('directory/whatever/file.txt', 'r')
file2 = open('directory/whatever/file2.txt', 'a')
line = file.readline()
file2.write(line + 'Thiswillprintonthenextline')
the message behind it will print on the next line
that's what I want to prevent
file2.write(line.rstrip('\n') + 'Thiswillprintonthenextline')
That is because the readline() function creates a string with the newline character at the end.
If you want to print it without changing the lines, simply strip the last character:
file2.write(line.rstrip("\n") + 'Thiswillprintonthenextline')
You could also strip it immediately when reading, so you don't have to do it every time you want to print it...
Just replace the line
line = file.readline()
with the line
line = file.readline().rstrip("\n")
and you're done. ;)
(Thanks to bruno desthuilliers for suggesting rstrip.)
I am trying to read the text within a .m file in Python and Python keeps reading a single character within the .m file as a line when I use file.readline(). I've also had issues with trying to remove certain parts of the line before adding it to a list.
I've tried adjusting where the readline is on for loops that I have set up since I have to read through multiple files in this program. No matter where I put it, the string always comes out separated by character. I'm new to Python so I'm trying my best to learn what to do.
# Example of what I did
with open('MyFile.m') as f:
for line in f:
text = f.readline()
if text.startswith('%'):
continue
else:
my_string = text.strip("=")
my_list.append(my_string)
This has only partially worked as it will still return parts of lines that I do not want and when trying to format the output by putting spaces between new lines it output like so:
Expected: "The String"
What happened: "T h e S t r i n g"
Without your input file I've had to make some guesses here
Input file:
%
The
%
String
%
Solution:
my_list = []
with open('MyFile.m') as f:
for line in f:
if not line.startswith('%'):
my_list.append(line.strip("=").strip())
print(' '.join(my_list))
The readLine() call was unnecessary as the for loop already gets you the line. The empty if was negated to only catch the part that you cared about. Without your actual input file I can't help with the '=' part. If you have any clarifications I'd be glad to help further.
As suggested by Xander, you shouldn't call readline since the for line in f does that for you.
my_list = []
with open('MyFile.m') as f:
for line in f:
line = line.strip() # lose the \n if you want to
if line.startswith('%'):
continue
else:
my_string = line.strip("=")
my_list.append(my_string)
Okay, this may sound like a stupid question, but I can't solve this problem...
I need remove all instances of backslash from downloaded file... But,
output.replace("\","")
doesn't work. Python considers "\"," a string, rather than "\" one string and "" the other one.
How can I remove backslashes?
EDIT:
New problem...
Originally, downloaded file had to be processed, which I did using:
fn = "result_cache.txt"
f = open(fn)
output = []
for line in f:
if content in line:
output.append(line)
f.close()
f = open(fn, "w")
f.writelines(output)
f.close()
output=str(output)
#irrelevant stuff
with open("result_cache.txt", "wt") as out:
out.write(output.replace("\\n","\n"))
Which worked okay, reducing file's content to only one line...
And finally ended with having this contents only:
Line of text\
Another line of text\
There\\\'s more text here\
Last line of text
I can't use the same thing again, because it would transform every line to a value in a list, leaving brackets and commas... So, I need to have:
out.write(output.replace("\\n","\n"))
out.write(output.replace("\\",""))
in the same line... How? Or is there another way?
Just escape the backslash with a backslash:
output.replace("\\","")
I have two text files (that are not equal in number of lines/size). I would like to compare each line of the shorter text file with every line of the longer text file. As it compares, if there are any duplicate strings, I would like to have those removed. Lastly, I would like write the result to a new text file and print the contents.
Is there a simply script that can do this for me?
Any help would be much appreciated.
The text files are not very large. One has about 10 lines and the other has about 5. The code I have tried (that failed miserably) is below:
for line in file2:
line1 = line
for line in file1:
requested3 = file('request2.txt','a')
if fnmatch.fnmatch(line1,line):
line2 = line.replace(line,"")
requested3.write(line2)
if not fnmatch.fnmatch(line1,line):
requested3.write(line+'\n')
requested3.close()
with open(longfilename) as longfile, open(shortfilename) as shortfile, open(newfilename, 'w') as newfile:
newfile.writelines(line for line in shortfile if line not in set(longfile))
It's as simple as that. This will copy lines from shortfile to newfile, without having to keep them all in memory, if they also exist in longfile.
If you're on Python 2.6 or older, you would need to nest the with statements:
with open(longfilename) as longfile:
with open(shortfilename) as shortfile:
with open(newfilename, 'w') as newfile:
If you're on Python 2.5, you need to either:
from __future__ import with_statement
at the very top of your file, or just use
longfile = open(longfilename)
etc. and close each file yourself.
If you need to manipulate the lines, an explicit for loop is fine, the important part is set(). Looking up an item in a set is fast, looking up a line in a long list is slow.
longlines = set(line.strip_or_whatever() for line in longfile)
for line in shortfile:
if line not in longlines:
newfile.write(line)
Assuming the files are both plain text, each string is on a new line delimited with \n newline characters:
small_file = open('file1.txt','r')
long_file = open('file2.txt','r')
output_file = open('output_file.txt','w')
try:
small_lines = small_file.readlines()
long_lines = long_file.readlines()
small_lines_cleaned = [line.rstrip().lower() for line in small_lines]
long_file_lines = long_file.readlines()
long_lines_cleaned = [line.rstrip().lower() for line in long_lines]
for line in small_lines_cleaned:
if line not in long_lines_cleaned:
output_file.writelines(line + '\n')
finally:
small_file.close()
long_file.close()
output_file.close()
Explanation:
Since you can't get 'with' statements working, we open the files first using regular open functions, then use a try...finally clause to close them at the end of the program.
We take the small file and the long file and first remove any trailing '\n' (newline) characters with .rstrip(), then make all the characters lower-case with .lower(). If you have two sentences identical in every aspect except one has upper case letters and the other doesn't, they wont' match. Forcing them lower case avoids that; if you prefer a case-sensitive compare, remove the .lower() method.
We go line by line in small_lines_cleaned (for line in...) and see if it is in the larger file.
Output each line if it is not in the longer file; we add the '\n' newline character so that each line will appear on a new line, insteadOfOneGiantLongSetOfStrings
I'd use difflib, it makes it easy to do comparisons/diffs. There is a nice tutorial for it here. If you just wanted the lines that were unique to the shorter file:
from difflib import ndiff
short = open('short.txt').readlines()
long = open('long.txt').readlines()
with open('unique.txt', 'w') as f:
f.write(''.join(x[2:] for x in ndiff(short, long) if x.startswith('-')))
Your code as it stands checks each line against the line in the other file. But that's not what you want. For each line in the first file, you need to check whether any line in the other file matches and then print it out if there are no matches.
The following code reads file two and checks it against file one.Anything that's in file one but not in file two will get printed and also written to a new text file.
If you wanted to do the opposite, you'd just get rid of the "not" from if statement below. So it'd print anything that's in file one and in file two.
It works by putting the contents of the shorter file (file two) in a variable and then reading the longer file (file one) line by line. Each line is checked against the variable and then the line is either written or not written to the text file according to it's presence in the variable.
(Remember to remove the stars surrounding the not statement if you wish to use it, or removing the not statement all together if you want it to print the matching words.)
fileOne = open("LONG FILE.ext","r")
fileTwo = open("SHORT FILE.ext","r")
fileThree = open("Results.txt","a+")
contents = fileTwo.read()
numLines = sum(1 for line in fileOne)
for i in range (numLines):
if **not** fileOne.readline(i) in contents:
print (fileOne.readline(i))
fileThree.write (fileOne.readline(i))
fileOne.close()
fileTwo.close()
fileThree.close()
I am using a Python script to find and replace certain strings in text files of a given directory. I am using the fileinput module to ease the find-and-replace operation, i.e., the file is read, text replaced and written back to the same file.
The code looks as follows:
import fileinput
def fixFile(fileName):
# Open file for in-place replace
for line in fileinput.FileInput(fileName, inplace=1):
line = line.replace("findStr", "replaceStr")
print line # Put back line into file
The problem is that the written files have:
One blank line inserted after every line.
Ctrl-M character at the end of every line.
How do I prevent these extra appendages from getting inserted into the files?
Your newlines are coming from the print function
use:
import sys
sys.stdout.write ('some stuff')
and your line breaks will go away
Use
print line,
or
file.write(line)
to fix extra newlines.
As of [Ctrl]-[M] - that is probably caused by input files in DOS encoding.
Instead of this:
print line # Put back line into file
use this:
print line, # Put back line into file
Change the first line in your for loop to:
line = line.rstrip().replace("findStr", "replaceStr")
Due to every iteration print statement ends with newline, you are getting blank line between lines.
To overcome this problem, you can use strip along with print.
import fileinput
def fixFile(fileName):
for line in fileinput.FileInput(fileName, inplace=1):
line = line.replace("findStr", "replaceStr")
print line.strip()
Now, you can see blank lines are striped.
For the update on Python 3.4, you can just use:
print(line, end = '')
to avoid the insertion of a new line.