Python: .strip() and .write() seem to produce whitespace - python

I wrote some code to let me type special characters without learning the commands for them so I wrote this:
file = open('test.txt', 'r+')
text = file.read()
text = text.replace('//a', 'Ä')
text = text.replace('//o', 'Ö')
text = text.replace('//u', 'Ü')
text = text.replace('/a', 'ä')
text = text.replace('/o', 'ö')
text = text.replace('/u', 'ü')
text = text.replace('/s', 'ß')
file.truncate(0) # Clears the file
file.write(text.strip()) # edit was .strip(''), made no diffence
print(text)
An example input would be 'n/achtes' which would become 'nächtes'
This sort of works but when I run the file I get a huge amount of blank space in the text file for example 'n/achtes' turns into:
' nächtes'
If I run the program a second time the output, on sublimetext 3, ends with nächtes but has 8 uncopyable copies of <0x00> in a different colour. The amount of blank spaces increases as well in the text file.

truncate(0) resizes the file to zero size, but the current position is not changed.
When writing the data, it is written at current position, so the rest of the file gets null bytes to "pad".
It is a better practice to use truncate() without parameter to truncate the file in the current position:
f.seek(0) # go to the beginning of the file
f.truncate() # truncate in current position

You could try to open the file twice, once for reading and once for writing.
filename = 'text.txt'
with open(filename, 'r') as f:
text = f.read()
print('-' * 20)
print('old text:')
print(text)
replacement_list = [
('//a', 'Ä'),
('//o', 'Ö'),
('//u', 'Ü'),
('/a', 'ä'),
('/o', 'ö'),
('/u', 'ü'),
('/s', 'ß'),
]
for s_old, s_new in replacement_list:
text = text.replace(s_old, s_new)
print('-' * 20)
print('new text:')
print(text)
with open(filename, 'w') as f:
f.write(text.strip())

Related

How to add quotes in the file

I have file below
123,PEN BOOK
124,BALL
125,BOOK
126,PENCIL BOOK
I need to add quotes
Expected out
"123","PEN BOOK"
"124","BALL"
"125","BOOK"
"126","PENCIL BOOK"
Assume you have a file test.txt with the following content:
123,PEN
124,BALL
125,BOOK
126,PENCIL
You can use a code like the following, to create a temp file with the content with quotes and replace the original file:
import os
with open("test.txt") as i: # open file for reading, i = input file
with open("temp.txt", "w") as o: # open temp file in write mode, o = output
for l in i: # read each line
o.write('"{}","{}"\n'.format(l.split(',')[0],l.split(',')[1].split('\n')[0]))
os.remove('test.txt') # remove the old file
os.rename('temp.txt','test.txt') # resave the temp file as the new file
Output:
"123","PEN"
"124","BALL"
"125","BOOK"
"126","PENCIL"
I've updated my answer to cover additional case of text containing spaces.
Seeing as you have a regex tag in your question, you can use something like this:
import re
text = """123,PEN
124,BALL
125,BOOK
126,PENCIL
123,PEN BOOK"""
new_text = re.sub(r'(\d+),([\w\s]+)$', r'"\1","\2"', text, flags=re.M)

How to remove content from a text file?

We have a text file with specific strings in it. How would we remove the strings from the file and save it as a new file?
File content before script runs:
This has foo in it.
This could also have foo in it.
There is none in here.
Competently morph 24/365 markets foo rather than inexpensive customer service.
File content after script runs:
This has in it.
This could also have in it.
There is none in here.
Competently morph 24/365 markets rather than inexpensive customer service.
Here is what we tried, but cannot figure out how to remove just the string and leave the rest of the content.
def main():
// my target string to remove
mystring = 'foo'
// open the file
f = open("myfile.txt", "r")
// second file to output content to.
f2 = open("output.txt", "w+")
// read file line by line
flines = f.readLines()
//iterate through lines
for x in flines:
// stuck here.
// how to find mystring in the line
// after removing mystring from line, append to f2
How about using re.sub():
import re
def main():
mystring = 'foo'
f = open("myfile.txt", "r")
f2 = open("output.txt", "w+")
flines = f.readLines()
result = re.sub(r"\s?{}\s?".format(mystring), " ", flines)
f2.write(result)

Add header and footer to each line in a file. Function returns only first line

I have a problem with some of my python code. I want it to open a file, with few lines of text, and add header + footer to each line in that file.
The problem is that 'create_output()' function returns only the first line with additional content. If I switch 'return' to 'print' at the end of this function it properly displays all lines from my file. What could be the reason? I want to understand what am I doing wrong here.
file_path = '/home/user/Desktop/text.txt'
file_path_edited = '/home/user/Desktop/text_new.txt'
header = 'http://'
footer = '.com'
def open_file():
opened_file = open(file_path)
return opened_file
def edit_file():
edited_file = open(file_path_edited, 'w')
return edited_file
def create_output():
for line in open_file():
line = line.strip()
edited_line = header+line+footer
to_file = edit_file()
to_file.writelines(edited_line)
to_file.close()
return edited_line
print (create_output())
OK, I changed it to something like this, now it works fine.
Thanks your feedback, now I know what I'm doing wrong.
file_path = '/home/user/Desktop/text.txt'
file_path_edited = '/home/user/Desktop/text_new.txt'
header = 'http://'
footer = '.com'
def CreateContent():
with open(file_path) as read_file:
with open(file_path_edited, 'w') as write_file:
for line in read_file.readlines():
new_line = "{}{}{}".format(header, line.strip(), footer)
print(new_line)
write_file.write("{}\n".format(new_line))
CreateContent()
You get only one line, because you reopen the write-file all the time instead of letting it open, "w" will truncate the file on open - so last line lives, rest is useless IO. Also you never close your reader afaics.
open(filename, mode) from https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files:
mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it’s omitted.
Do not split the file open into extra functions, use with open(...) as bla: bla.write(...) so they get closed as soon as you leave the block or some exception happens.
Use string-formatting - either 'this {} ist repleaced with'.format("something") or the inline variant - see below.
def create_output():
modLines = []
with open('/home/user/Desktop/text.txt',"r") as reader, \
open('/home/user/Desktop/text_new.txt',"w") as writer:
for line in reader:
line = line.strip().rstrip('\n') # rstrip might be better if you only cut \n
modline = f'http://{line}.com' # 3.6 inline string formatting, for 2.7 use
modLines.append(modline) # 'http://{}.com'.format(line)
writer.write(modline+"\n") # write does not autoappend \n
return modlines # return a list of written https...
print (create_output())
Should do the trick.
Links:
Format string syntax
Reading and writing files
You could further improve your code as follows:
file_path = '/home/user/Desktop/text.txt'
file_path_edited = '/home/user/Desktop/text_new.txt'
header = 'http://'
footer = '.com'
def CreateContent():
with open(file_path) as read_file, open(file_path_edited, 'w') as write_file:
for line in read_file:
write_file.write("{}{}{}\n".format(header, line.strip(), footer))
CreateContent()

computer keeps crashing with read/writing files

I am not sure why my computer keeps crashing when using this program I tried editing it by closing the file each time I run but it still crashes.
f = open("testfile.txt", "r")
text = f.read()
text = str(text)
looper= text.count(",")
f.close()
god= open("dog.txt","w+")
god.write("Passwords")
god.close()
block=""
for count in range(looper):
first= (text.find(','))
second= (text.find(':'))
block = text[first:second]
text = text[:first] + text[second:]
god= open("dog.txt","a")
god.write(block)
god.close()
I am trying to take items from a text file and format them into a new text file.
EDITED CODE:
f = open("testfile.txt", "r")
text = f.read()
text = str(text)
looper= text.count(",")
f.close()
god= open("dog.txt","w+")
god.write("Testing")
god.close()
for count in range(looper):
block = ""
first= (text.find(':'))
second= (text.find(','))
block= text[first:second]
text = text[second:]
text = text[first:]
print (block)
god= open("dog.txt","a")
god.write(block)
god.close()
In the first section of code I didn't have the string block clearing and I didn't have the string text updating so what would happen is that each time it looped block would duplicate it self
EX:
loop1) :helloworld
loop2) :helloworld:helloworld
loop3) :helloworld:helloworld:helloworld
making those two changes fixed the problem.

How to read a message using TKInterface?

This is the "click me" function which runs when the button on the interface is clicked.
def click_me():
file_name = file_name_entry.get()
the_file_name = str(file_name)
open(the_file_name, "r")
imp_message = file.read(the_file_name)
There is a grey line beneath the_file_name in brackets and when hovered over says: passing str instead of file - is this intentional?
password_output.delete(0.0, END)
password_output.insert(END, imp_message)
The relevant TKInter code is as follows...
file_name_entry = Entry(encrypt_frame, width=20)
file_name_entry.grid(column = 1, row = 1, sticky = W)
Button(encrypt_frame, text= "Submit", command = click_me).grid(column = 2, row = 1)
The error output is:
IOError: Errno22 invalid mode ('r/) or filename ""
Your code is close, but you're trying to read from the file name rather than the opened file object. Change your code to this:
file=open(the_file_name, "r")
imp_message = file.read()
Better yet, use a context manager which will automatically close the file when you're done:
with open(the_file_name, "r") as f:
imp_message = f.read()
The python documentation has a nice tutorial on reading and writing files:
Python 2 - Reading and writing files
Python 3 - Reading and writing files
Unrelated to the actual question, there are a couple problems with your code.
First, there's no need for this statement:
the_file_name = str(file_name)
In this case, file_name is already a string since it is coming from an Entry widget.
Second, the index 0.0 in the statement password_output.delete(0.0, END) is incorrect. Text widget indexes must be strings in the form of <line>.<column>, and lines start at 1 (one). The proper index for the first character is the string "1.0".

Categories