0

I use the '/' special character to split each word in the text file. The code output is as follows:

Mariam / AI / DS / ML


Steeve / DM / CO / DBMS / ML 

Peter / DS / CO / MDS / ML 

Stella / AI / DS / ML / DSAD 

Martin / AI / ML / DS / MDS 

I'm aiming to separate every word in the file. The code I developed to solve this problem is available below:

import os

desktop = os.path.join(os.path.expanduser("~"), "Desktop")
filePath = os.path.join(desktop, "inputPS13.txt")
file2 = open(filePath)
line1=file2.readline()
print("file is opened")

while(line1!=""):
    print(line1)
    line1=file2.readline()
   
for line in file2:
    for word in line.split("/"):
           print(word)

1 Answer 1

0
with open("input.txt", "r") as file:

    # read file and remove new line characters
    lines = [line.strip() for line in file.readlines()]

    # remove empty strings
    lines = [line for line in lines if not line == ""]

for line in lines:

    # prints a list of of every word in a line
    print(line.split(" / ")) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.