[Python] How to read then parse with split and write into a text file?

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm struggling to get readline() and split() to work together as I was expecting. Im trying to use .split(')') to cut down some data from a text file and write some of that data to a next text file.

    I have tried writing everything from the line. I have tried [cnt % 2] to get what I expected.

    line = fp.readline()
    fw = open('output.txt', "w+")
    cnt = 1
    while line:
    print("Line {}: {}".format(cnt, line.strip()))
    line = fp.readline()
    line = line.split(')')[0]
    fw.write(line + "\n")
    cnt += 1


    Example from the text file im reading from.

    WELD 190 Manufacturing I Introduction to MasterCAM (3) 1½ hours lecture - 4½ hours laboratory Note: Cross listed as DT 190/ENGR 190/IT 190 This course will introduce the students to MasterCAM and 2D and basic 3D modeling. Students will receive instructions and drawings of parts requiring 2- or 3-axis machining. Students will design, model, program, set-up and run their parts on various machines, including plasma cutters, water jet cutters and milling machines. WELD 197 Welding Technology Topics (.5 - 3)

    I'm very far off from actually effectively scraping this data but I'm trying to get a start.

    My goal is to extract only class name and number and remove descriptions.

    Thanks as always!

    Continue reading...

Compartilhe esta Página