Is there a way to split lines from text when turning into an array in python

Question:

Is there an alternative way to split lines from a text file without using the n in the code? When I use n to split the text lines, it’s a little bit annoying that n also got printed out in the array lines. here is the code that I use so that the text lines can be split when turning into an array

Combined = open("dictionaries/Combined.txt", 'r', encoding="utf-8")
Example = open("dictionaries/examples.txt", 'w', encoding="utf-8")

for web in Combined:
    Combined_result = re.search(r"([w-]+.)*w+[w-]*", web)
    Text = (Combined_result.group())
    Example.write((Text+'.web'+'.co.id'+"n"))

Combined.close()
Example.close()

sample = open("dictionaries/examples.txt", 'r', encoding="utf-8")
websamples = sample.readlines()

websamples

And this is the output that I got, the n doesn’t look good at all when seeing the array output.

['auth.web.co.idn',
 'access.web.co.idn',
 'account.web.co.idn',
 'admin.web.co.idn',
 'agree.web.co.idn',
 'blue.web.co.idn',
 'business.web.co.idn',
 'cdn.web.co.idn',
 'choose.web.co.idn',
 'claim.web.co.idn',
 'cl.web.co.idn',
 'click.web.co.idn',
 'confirm.web.co.idn',
 'confirmation.web.co.idn',
 'connect.web.co.idn',
 'download.web.co.idn',
 'enroll.web.co.idn',
 'find.web.co.idn',
 'group.web.co.idn',
 'http.web.co.idn',
 'https.web.co.idn',
 'https-www.web.co.idn',
 'install.web.co.idn']

Thanks!

Asked By: Tsukuru

||

Answers:

From another question: Getting rid of n when using .readlines() .

A good approach is instead of using .readlines(), which as you’ve noticed includes n characters, and instead using something like.

with open('dictionaries/examples.txt') as f:
    websamples = [line.rstrip() for line in f]

This uses a couple of ‘fancier’ Python features, that help make it more pythonic.

  • A context manager with the with statement, which automatically closes the file handle at the end of the block, without having to manually call .close().
  • A list comprehension used to create a new array by calling .rstrip() on each line as it is read from the input file.
Answered By: Damien Ayers
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.