-1

I have a txt file that I need to convert into a table. If I have a case like this:

---------------------------------------------
|apple|very good|every day|fruit
|chocolate|not so good|just\n
some times|snack
|bread|good|every day|whole|carbs
---------------------------------------

I splitted the file on the '|' but the new line is a problem I cannot overcome, how can I join the two lines?

with open("ridotto.txt", encoding='latin-1') as f:     
    new_list=[]
    for line in f:
        if line.startswith("-"):
            line.replace("-", "")
        else:
            new_list.append(line.replace('\n', ' ').split('|'))

When I do this I get as a result:

[apple,very good,every day,fruit,chocolate, not so good, just ,some times, snack,bread, good, every day, whole, carbs]

while I want just some times to be one singular element Note: the \n is not literal

anea
  • 25
  • 2
  • 1
    Is that a literal `\n` in the file, not a newline? – Barmar Nov 22 '22 at 15:31
  • Judging by the answers, your question is not clear. I believe what you are actually trying to do is consider the two lines starting with `chocolate` as one, not to remove a newline chatacter – Tomerikoo Nov 22 '22 at 15:35
  • @Tomerikoo yes, I thought that by removing the new line they would collapse together. I will change the question to make it clearer – anea Nov 22 '22 at 15:37
  • Hi again...What do you mean you want to convert into table? How your expected output should looks like? How your presnt output looks like? – Bhargav - Retarded Skills Nov 22 '22 at 15:38
  • You are already reading the file line-by-line. All you do is remove a new line character from the end of a string. It doesn't affect the file or merge lines. You could do `f.read()` and manipulate the whole file as one continuous string, but then you would lose all new-line characters and the whole file will become one line – Tomerikoo Nov 22 '22 at 15:38
  • `line.replace("-", "")` doesn't do anything. You need to assign the result somewhere. And if the line is all `-`, this is just an empty string. – Barmar Nov 22 '22 at 15:39
  • That can't be what you get as your code produces a list of lists... – Tomerikoo Nov 22 '22 at 15:42

5 Answers5

0

Sometimes the \n is actually \r\n under the hood

try this if it helps

new_list.append(line.replace('\n', ' ').replace('\r\n', ' ').split('|'))
Rajesh Kanna
  • 113
  • 6
0

If you are trying to remove a literal newline, then just use two backslashes: replace('\\n', ' '). Python uses the backslash as an escape character and so you have to escape the backslash itself if you want to use a literal backslash.

David
  • 38
  • 5
0

for your code, I recommend you to do this

with open("ridotto.txt", encoding="latin-1") as f:
    new_list = []
    for line in f:
        if "\\n" in line:
            new_list.append(line.replace("\\n\n", "").split("|"))
        elif not line.startswith("-"):
            new_list.append(line.replace("\n", "").split("|"))
Null
  • 1
  • 2
0

Read text file. & replace items one by one accordingly.

with open('text.txt', 'r') as file:
    data = file.read().replace('\n', '')

data = data.replace("\\n", " ")
data = data.replace("-", " ")
data = data.strip()
my_list = data.split("|")
my_list = [i for i in my_list if i]  # Remove empty elements

print(my_list)

Gives #

['apple', 'very good', 'every day', 'fruit', 'chocolate', 'not so good', 'just some times', 'snack', 'bread', 'good', 'every day', 'whole', 'carbs']
  • How will that help to make a table from the data? Now it's just a flat list... – Tomerikoo Nov 22 '22 at 16:02
  • That's I thought at first...But later OP posted list saying he got output at one of the list elemnt as ` just , some times` ...This because `\n` litteral is present. He want to modify code such that he want entire elemnt as ` just some times `....So posted according to expected outputs...I – Bhargav - Retarded Skills Nov 22 '22 at 16:05
0

Maybe the following logic might help you, if what you want is to combine 2 lines, when the first line ends with \n in the file

from io import StringIO

with open("ridotto.txt", encoding='latin-1') as f:     
    new_list=[]
    
    ## if there is \n in end of text line, then remove the newline character on that line ##
    c = f.read().replace("\\n\n",' ')
    
    ## Reading string like a file ##
    with StringIO(c) as file: 
        for line in file:
            if line.startswith("-"):
                continue
            else:
                new_list.append(line.replace('\n', '').split('|'))
SoulSeeke
  • 11
  • 2