我使用“/”特殊字符来分割文本文件中的每个单词。代码输出如下:
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)发布于 2021-12-13 18:50:26
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(" / ")) https://stackoverflow.com/questions/70338664
复制相似问题