Adding a newline to the printed data from an imported file

Viewed 33

I made the following code, which imports a file and prints its content :

import pandas as pd

file = r"..\test.xlsx"

try:
    df = pd.read_excel(file)
    #print(df)

except OSError:
    print("Impossible to read", file)

test =
    df['Date'].map(str) + ' | ' \
    + df['Time'].map(str) + ' | ' \
    + df['Description'].map(str) + ' | ' \
    + '\n'
    print(test)

The output is (Edit : I precise that it is printed in an html file) :

20/01 | 17:00 | Text description here1 17/01 | 11:00 | Text description here2 16/01 | 16:32 | Text description here3 <- In orange when the the "Urgence" is equal to 3

But what I want is :

20/01 | 17:00 | Text description here1
17/01 | 11:00 | Text description here2                                                            
16/01 | 16:32 | Text description here3

I added a new line at the end of my statement + '\n' but it doesn't seem to change anything. How should I proceed ? Thank you.

Edit : I believe that the problem comes from the fact that the entire file is printed, and not line by line so it doesn't add the newline to each line. So I made this code :

test = []
for index, row in df.iterrows():
    x = row['Date'] + ' | ' + row['Description'] + '\n'
    test.append(x)
print(test)

But the result is the same..

1 Answers

Try this:

test = df['Date'].map(str) + ' | ' +
       df['Time'].map(str) + ' | ' +
       df['Description'].map(str) + ' | '

list(map(lambda x: print(x), test))

I removed the end of the test string and added the print function.

Let me know if there is any problem :)

Related