简体   繁体   English

我需要在 txt 文件中打印一行的特定部分

[英]I need to print the specific part of a line in a txt file

I have this text file that reads,,Janitors, 3,, ,,Programers, 4,, and,,Secretaries, 1,, and all of these are on different lines.我有这个文本文件,内容为,看门人,3,,,程序员,4,和,秘书,1,所有这些都在不同的行上。 I need to print out Janitor seperate from the number 3, and this has to work for basicaly any word and number combo.我需要打印出与数字 3 分开的看门人,这必须适用于基本上任何单词和数字组合。 This is the code I came up with and, of course, it doesnt work.这是我想出的代码,当然,它不起作用。 It says,,substring not found,,它说,substring 未找到,,

File = open("Jobs.txt", "r")
Beg_line = 1
for lines in File:
    Line = str(File.readline(Beg_line))
    Line = Line.strip('\n')
    print(Line[0: Line.index(',')])
    Beg_line = Beg_line + 1
File.close()

Try running the following code:尝试运行以下代码:

file = open("Jobs.txt", "r")
lines = file.read().split('\n')
for line in lines:
    print(line.split(' ')[0])
file.close()

This will give the following output:这将给出以下 output:

Janitors
Programers
Secretaries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM