如何编写一个代码,打印出第一行的每一行的第一个单词?卡住 [英] How do I write a code that prints the first word of every line expect the first line? Stuck

查看:98
本文介绍了如何编写一个代码,打印出第一行的每一行的第一个单词?卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打印非常行的第一个单词(期望第一行),但我不断收到synthax错误。它来自我创建的文本文件。这是代码/文字



我们很饿。 
有一个苹果。
有橙色。
现在我们口渴了。





我的尝试:



 filename =   firstthing.txt 
file = open(filename)
text = file.read
lines = text.split(' \ n'

行:
line.split( 1 )[ 0 ]
print 有以下几行:





我做错了什么?我该怎么写呢?

解决方案

基于这些:

python - 如何跳过`for`循环中的第一个元素? - 堆栈溢出 [ ^ ]

在python中跳过for循环的第一个条目? - 堆栈溢出 [ ^ ]

您需要使用下一个功能 [ ^ ]



 lines = text.split('  \ n'
next(行)
in 行:
print line.split( 1 )[ 0 ]


im trying to print the first word of very line (expect the first line) but i keep getting synthax error. It's from a text file i created. Here is the code/text

We are hungry.
There is an apple.
There is an orange.
Now we are thirsty.



What I have tried:

filename = "firstthing.txt" 
file = open(filename)
text = file.read
lines = text.split('\n')

for line in lines:
     line.split(" ",1)[0]
     print " There are the lines:"



What am i doing wrong? How do i write it properly?

解决方案

Based on these:
python - How to skip first element in `for` loop? - Stack Overflow[^]
Skip first entry in for loop in python? - Stack Overflow[^]
you need to use next function[^]

lines = text.split('\n')
next(lines)
for line in lines:
     print line.split(" ",1)[0]


这篇关于如何编写一个代码,打印出第一行的每一行的第一个单词?卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆