Python Forum
experimenting extracting (\ n) character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
experimenting extracting (\ n) character
#1
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.

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")
Reply
#2
use strip()
for example:
zzstr = 'I am a string with lf at end.\n'
print(zzstr.strip())
Reply
#3
Thank you,
that's very helpful for my applications.
Reply
#4
If you want to remove '\n' at the end of the line, use rstrip('\n'). Also you can simulate a file directly in your program by using a io.StringIO instance
import io

ifile = io.StringIO("""\
Enim omnis eius enim aut. Quos et non fugiat. Quia aperiam pariatur
provident eius aliquid sit natus. Facere alias quia recusandae neque
nam quia nostrum enim. Illum voluptatem nesciunt nisi id a qui.
Aut magni accusantium ea porro et sunt velit.

Assumenda minus voluptate et veritatis nam. Quidem quidem sequi
vero et sunt. Ipsum odit ut voluptate unde quisquam in.

Quas optio odio fugit harum iste quam qui corrupti. Aut eum
    accusantium dicta architecto. Iste doloremque voluptate
    repellendus. Id nulla distinctio blanditiis provident quis non
    non. Ut sint maxime eos quaerat facere aspernatur et.
    Totam iste animi cum adipisci deleniti.
""")

for item in ifile:
    print(item.rstrip("\n"))
Reply
#5
Thank you, these are great solutions for my applications,
very much appreciated.
You guys are very helpful, thanks again.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020