【发布时间】:2022-01-17 03:53:14
【问题描述】:
我使用“/”特殊字符来分割文本文件中的每个单词。代码输出如下:
Mariam / AI / DS / ML
Steeve / DM / CO / DBMS / ML
Peter / DS / CO / MDS / ML
Stella / AI / DS / ML / DSAD
Martin / AI / ML / DS / MDS
我的目标是分隔文件中的每个单词。我为解决这个问题而开发的代码如下:
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)
【问题讨论】:
标签: python file string-parsing