[Python] Printing string item prints individual characters

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have a text file with five lines. I open and read the lines into an array then print the array successfully but when I print a single item in the array it prints individual characters. I need to use the five input lines as individual strings in the code.

    #!/usr/bin/python3

    # read the configuration file (from setup.py)

    try:
    f = open('interface.txt', encoding="utf-8")
    vars = f.read()
    f.close()
    except IOError:
    print("oops cant open interface.txt")
    exit ()

    # debug
    print ('\nthis works prints five items\n')
    print (vars)

    print ('\nthis prints every individual character in the file\n')
    for xx in vars:
    print (xx)


    output:

    jbh@junkbox-2:~/Desktop/Athens/Volume1/Chapter00$ ./readInterface.py

    this works prints five items

    Chapter 00 - Introduction
    index.html
    ../Chapter10/index.html
    ../Chapter01/index.html
    Chapter00.csv


    this prints every individual character in the file

    C
    h
    a
    p
    t
    e
    r

    0
    0

    -

    I
    n
    t
    r
    o
    d
    u
    c
    t
    i
    o
    n
    -- etc. ---

    Continue reading...

Compartilhe esta Página