0

for a .txt looking like the below (already opened):

EFJAJCOWSS
SDGKSRFDFF
ASRJDUSKLK
HEANDNDJWA
ANSDNCNEOP
PMSNFHHEJE
JEPQLYNXDL

Return a board that will contain each row on a different line. Newlines are not included in the board. I have the below code:

def read_board(board_file):
    file = open(board_file, 'r')
    line = file.readline()
    newList = ''
    while line != '':
        newList = newList + line.strip('\n') + '\n'
        line = file.readline()
    return newList

The result is : 'EFJAJCOWSS\nSDGKSRFDFF\nASRJDUSKLK\nHEANDNDJWA\nANSDNCNEOP\nPMSNFHHEJE\nJEPQLYNXDL\n'

Any tips on how I can get this to list each row on a different line, as mentioned above? +'\n' doesn't do the trick...

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
marius
  • 1

1 Answers1

0

file.readlines() gives list of lines

lst_lines = file.readlines()

lst_lines will be like: ['EFJAJCOWSS\n','SDGKSRFDFF\n'........]

Now you can iterate over lst_lines list with for loop line[0],line[1] is different line

for line in lst_lines:
  print(line)
Himanshu dua
  • 2,496
  • 1
  • 20
  • 27