1

I have a txt file of only a continuous string of characters MNHY...

I want to have the output of ['M','N','H','Y'...]

I have attempted this code below and various forms of it, but I can only convert the txt file as one continuous list.

def text_check(filename):
    my_file = open(filename,"r")
    print(my_file.read().splitlines())
text_check("demo_text.txt")

How can I convert the .txt file to this ['M','N','H','Y'...] format?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
nosebig
  • 35
  • 4
  • 1
    You know how to read lines from a file (though maybe you want to store the lines read in a variable before printing them..), now look up how to iterate over the items in a string (or how to convert it to a `list` of characters) – GPhilo Nov 16 '21 at 12:43
  • Does this answer your question? [Break string into list of characters in Python](https://stackoverflow.com/questions/9833392/break-string-into-list-of-characters-in-python) – Mike Scotty Nov 16 '21 at 12:44
  • okay thanks, i'll try to focus on looking this up! I feel like I am just getting easily lost due to my inexperience. – nosebig Nov 16 '21 at 12:46
  • No worries , learning how (and where) to look things up is the frst step in learning how to code :) It takes a while, but it gets better very quickly! Normally, for very basic questions such as this one, there's always an answer already, the trick is learning the right search terms ;) – GPhilo Nov 16 '21 at 12:49
  • @MikeScotty I wish it were that easy! Listing it in a simple way just returns an empty list. – nosebig Nov 17 '21 at 01:33
  • @GPhilo I think trying to improve my ability to search for solutions is also something i need to develop. It can be a bit of a minefeld even trying to search in the right way, any tips on this would be appreciated! – nosebig Nov 17 '21 at 01:35

2 Answers2

1

Assuming the file contains

MNYH…

you can use:

with open(filename) as f:
    out = list(f.read().strip())
print(out)

output: ['M', 'N', 'H', 'Y', '…']

multi-line files

MNHY
ABCD
with open(filename) as f:
    out = [c for l in f for c in l.strip()]

output: ['M', 'N', 'H', 'Y', 'A', 'B', 'C', 'D']

mozway
  • 194,879
  • 13
  • 39
  • 75
  • This returned a TypeError: Not Iterable on the object. Thanks for the suggestion though. – nosebig Nov 17 '21 at 01:37
  • @nosebig which python version so you have? Are you sure the file only has a single line? – mozway Nov 17 '21 at 05:51
  • 3.7 It's multiple lines on the file, sorry if this wasn't clear from the .... continuation – nosebig Nov 17 '21 at 11:23
  • @nosebig it should still work, but include the newline characters in the output. Can you check again? I updated the answer to handle the multi-line case – mozway Nov 17 '21 at 11:27
0

Simply use list() method.

The list() method in python will convert a continous string into seperate character.

In other words, the codes that work for you are:

def text_check(filename):
    my_file = open(filename,"r")
    li_chars = list(my_file.read().splitlines()[0])
    print(li_chars)
    my_file.close()
text_check("demo_text.txt")
  1. The list() method will do the spliting when it accepts a string, so I add [0] to splitlines().

  2. I add my_file.close() at the end of text_check().


Improvements

def text_check(filename):
    with open(filename,"r") as f:
        li_chars = list(f.read().splitlines()[0])
        print(li_chars)
text_check("demo_text.txt")

The with statement provides a way for ensuring that a clean-up is always used.

zeit
  • 347
  • 2
  • 13
  • Thanks, this returned the array with individual characters from the .txt file that i was looking for. – nosebig Nov 17 '21 at 01:39