Parse a plain text file into a CSV file using Python

后端 未结 2 1557
星月不相逢
星月不相逢 2020-12-09 23:08

I have a series of HTML files that are parsed into a single text file using Beautiful Soup. The HTML files are formatted such that their output is always three lines within

2条回答
  •  渐次进展
    2020-12-10 00:11

    Perhaps I didn't understand you correctly, but you can do:

    file = open("extracted.txt")
    
    # if you don't want to do .strip() again, just create a list of the stripped 
    # lines first.
    lines = [line.strip() for line in file if line.strip()]
    
    for i, line in enumerate(lines):
        csv.SetCell(i % 3, line)
    

提交回复
热议问题