0

I have a problem with a python program. In this program I have to take strings from a file and save it to a list. The problem is that in this file some strings occupy more lines.

The file named 'ft1.txt' is structured like this:

'''

home

wo

rk

''''


sec

urity

'''

inform

atio

n

'''

Consequently opening the file and doing f.read () I get out:

" \n\nhome\nwo\nrk\n\nsec\nurity\n\ninform\nation\nn ".

I execute the following code:

 with open('ft1.txt', 'r') as f: #i open file 
     list_strin = f.read().split('\n\n') #save string in list

I want output [homework, security, information]. But the actual output is [home\nwo\nrk, sec\nurity, inform\nation\nn]

How can I remove the special character "\n" in individual strings and merge them correctly?

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Matis23
  • 19
  • 1
  • 6
  • You already know how to remove the `'\n'` from a string (aka `split` that string), so your actual question now is "How to concatenate a list returned by `split` into a string?", right? – ForceBru Nov 10 '19 at 17:46
  • Maybe easier with replace, than split and rejoin. – Christian Sloper Nov 10 '19 at 17:48
  • My problem is that I want a list of output strings but the problem is that I get a list in which the strings inside them have the special character 'as in the file the strings are on several lines. – Matis23 Nov 10 '19 at 17:51

1 Answers1

0

You have \n in string. Remove it :-)

list_strin = [x.replace('\n', '') for x in f.read().strip().split('\n\n')]

readline solution:

    res = []
    s = ''
    with open('ft1.txt', 'r') as f:
        line = f.readline()
        while line:
            line = line.strip()
            if line == '':
                if s:
                    res.append(s)
                s = ''
            else:
                s += line
            line = f.readline()
    print(res)
Quazer
  • 353
  • 1
  • 8
  • OK thanks. I would like to ask you if there is a way to do it without the for, but in a more direct way. Because in my program I have to act on several files that are always bigger, with bigger and bigger strings. With the for I would slow down the program too much. (I'm sorry I didn't specify this in the original application) – Matis23 Nov 10 '19 at 18:00
  • Hmmm... Use readline, concatenate to string, append to list if two empty line. For very large files please see https://stackoverflow.com/questions/16669428/process-very-large-20gb-text-file-line-by-line – Quazer Nov 10 '19 at 18:05
  • I thought about readline () but I don't know how to use it. If I use it I always have to use a for loop that sees all the lines in the file. The fact is that I don't know how to specify to python that when it sees two or more full lines then it must concatenate them in a string and insert them in the list and when instead it finds an empty one it must pass to the next string. (I thank you for the suggestion on the big files but I have to deal with files at most a few mega max. This is a university task) – Matis23 Nov 10 '19 at 18:21
  • Thank you very much. Forgive my ignorance but I would like to ask you: is this code redundant? Because I didn't understand it very well. If it's not too much trouble, could you explain it to me? Otherwise no problem I try to figure it out for myself. :-) – Matis23 Nov 10 '19 at 18:45
  • No, this code is not reduntant. We open the file. And ```line``` = first line of the file. After, ```while line``` is a cycle "while", while line is not empty. In the cycle we strip the line (delete pre- and post- spaces) and use condition for empty line: \n\n is it a empty line. If line is empty (\n\n) we append to list ```res``` string ```s``` and set ```s``` to empty line, else contcatenate to s readed line (```s+=line```). Last line of the cycle is read next line from file – Quazer Nov 10 '19 at 19:15
  • You helped me a lot. Thank you, thank you very much – Matis23 Nov 10 '19 at 20:25