with open("my_file.txt", "r") as f:
next(f) # used to skip the first line in the file
for line in f:
words = line.split()
if words:
print(words[0])
将输出:
a-1,
b-2,
c-3,
我想让它从文件中输出/读取这个:
a-1, b-2, c-3,
将单词保存到列表中,然后用空格连接:
with open("my_file.txt", "r") as f:
first_words = []
next(f)
for line in f:
words = line.split()
if words:
first_words.append(words[0])
print(' '.join(first_words))
输出:
a-1, b-2, c-3,