Python 在Excel工作表中逐行打印文本文件

Python 在Excel工作表中逐行打印文本文件,python,excel,pandas,Python,Excel,Pandas,我试图逐行读取文本文件,然后逐行将其打印到excel工作表中 这是我到目前为止所拥有的 for x in ABC: print(f"{x}:") sheet1[cellLocLastRow('A')] = f"{x}:" try: with open(f"./{x}/Log.txt") as f: textRead= (f.read()) print

我试图逐行读取文本文件,然后逐行将其打印到excel工作表中

这是我到目前为止所拥有的

for x in ABC:
    print(f"{x}:")
    sheet1[cellLocLastRow('A')] = f"{x}:"
    try:
        with open(f"./{x}/Log.txt") as f:
            textRead= (f.read())
            print(textRead)
            sheet1[cellLocLastRow('A')] = textRead
    except FileNotFoundError:
        print("File does not exist")
        sheet1[cellLocLastRow('A')] = "File does not exist"
它将文本文件打印到excel工作表中,但所有内容都在一行中,如下所示

但是我希望我的文本文件像这样打印出来

如果您想知道我为什么要使用
[cellLocLastRow('A')]
,我会使用它而不是
[A17]
,因为我正在将未知长度的文档打印到excel工作表中,因此它会计算行数

def cellLocLastRow(colChar):
    global lastRow
    curRow = lastRow
    lastRow += 1
    return cellLoc(colChar, curRow)
文本文件格式如下:

TestName: TestName

TestName: Info::Info::Info::Info::f###::##.#ns
Total Errors: #

TestName: Info::Info::Info::Info::f###::##.#ns
Total Errors: #

TestName: Info::Info::Info::Info::f###::##.#ns
Total Errors: #

您是否尝试了
f.readlines()
方法

with open(text, 'r') as f: 
content1 = f.readlines()

此脚本将返回一个包含所有文件行的列表,然后您可以轻松地执行任何操作。

使用pylightxl这非常简单

pip安装pylightxl


我收到以下错误:ValueError:无法将[]转换为ExcelI我仅将其更新为readlines(),而没有strip。它应该是成功的,它的python内置函数。
lines = []
with open(“textfile.txt”) as f:
    line = f.readline()
    if not line:
        break
    lines.append(line)


import pylightxl as xl

db = xl.Database()

db.add_ws("Sheet1", {})

for i, line in enumerate(lines, start=1):
    db.ws("Sheet1").update_index(i, 1,line)

xl.writexl(db, “output.xlsx”)