[英]Adding a newline to the printed data from an imported file
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): output 是(编辑:我准确地说它打印在 html 文件中):
20/01 |
20/01 | 17:00 |
17:00 | Text description here1 17/01 |
此处有文字说明1 17/01 | 11:00 |
11:00 | Text description here2 16/01 |
此处有文字说明2 16/01 | 16:32 |
16:32 | Text description here3 <- In orange when the the "Urgence" is equal to 3
此处的文字说明3 <- 当“紧急度”等于 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.我在语句的末尾添加了一个新行
+ '\n'
但它似乎没有改变任何东西。 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..但是结果是一样的。。
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.我删除了测试字符串的末尾并添加了打印 function。
Let me know if there is any problem:)让我知道是否有任何问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.