我是初学者.
每次awk '{print $12}'在Python脚本中的文本文件(类似于Bash)中找到某个单词时,我需要从一个巨大的平面文本文件中提取第12列的内容.
到目前为止我有(例如,不是真正的代码):
word = a_word
for a_word in data:
column12 = data.split()
print(column12[11])
Run Code Online (Sandbox Code Playgroud)
我认为我应该从0开始计数而不是1,尽管我可能不正确.
此外,for这种代码的循环是否正确?
谢谢!
遍历打开的文件对象:
with open('somefile') as infile:
for line in infile:
print(line.split()[11])
Run Code Online (Sandbox Code Playgroud)
所以,是的,使用for循环并使用基于0的索引.
| 归档时间: |
|
| 查看次数: |
19684 次 |
| 最近记录: |