Nov-20-2018, 11:54 PM
I'm a beginner in python, Learning how to write small routines for a main program I'm working on,
I needed to find a way to eliminate the (\n ) char. when working with
.txt files, because the (\n ) char. messes up my main program, when
I read the file back in.
Here is a snippet I made as a reference for myself to look at, when
I encounter this need again.
Because I try not to write any code for this forum, that creates a file on
someones computer, without there knowing it, I wrote this snippet, requiring
the user to create there own file first. It is a txt file, called "list.txt"
to make this work.
I needed to find a way to eliminate the (\n ) char. when working with
.txt files, because the (\n ) char. messes up my main program, when
I read the file back in.
Here is a snippet I made as a reference for myself to look at, when
I encounter this need again.
Because I try not to write any code for this forum, that creates a file on
someones computer, without there knowing it, I wrote this snippet, requiring
the user to create there own file first. It is a txt file, called "list.txt"
to make this work.
import os class klass: '"docstring'" def_init_(self): self.attribute="var" if len(self.attribute) <2: pass #you need to create a file called "list.txt" before running this sub program. list=[] listnew=[] templist="list.txt" file=open(templist,"r") for item in (file): list.append(item) file.close() print("") print ("Before extraction ") print ("Notice the (\ n) at the end of each item") print("--------------------------") print("") print (list) print("") print("Notice the spaces in between the rows") print("") #This is the block of code to do the extraction of the (\n) char. for item in list: print (item) long=len(item) extr=(long-1) itemnew=(item[0:extr]) listnew.append(itemnew) #This ends the block of code for the extraction function. print("") print ("After extraction of ( \ n) ") print("--------------------------") print("") print(listnew) print("") for item in (listnew): print (item) print ("") savefile="list.txt" file=open(savefile,"w") for item in (listnew): file.write((item)+"\n") file.close() print ("This Routine works")