如何将每一行的第一个单词读入文件中的一行
How to read first word of every line, into one line in a file
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,
我想要output/read文件中的这个:
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,
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,
我想要output/read文件中的这个:
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,