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?

6
  • 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) Commented Nov 16, 2021 at 12:43
  • Does this answer your question? Break string into list of characters in Python Commented Nov 16, 2021 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. Commented Nov 16, 2021 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 ;) Commented Nov 16, 2021 at 12:49
  • @MikeScotty I wish it were that easy! Listing it in a simple way just returns an empty list. Commented Nov 17, 2021 at 1:33

2 Answers 2

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']

Sign up to request clarification or add additional context in comments.

4 Comments

This returned a TypeError: Not Iterable on the object. Thanks for the suggestion though.
@nosebig which python version so you have? Are you sure the file only has a single line?
3.7 It's multiple lines on the file, sorry if this wasn't clear from the .... continuation
@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
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.

1 Comment

Thanks, this returned the array with individual characters from the .txt file that i was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.