擅长:python、mysql、java
<p>您使用哪种分隔符?也就是说,是什么分隔了你的列?</p>
<p>我假设您使用逗号分隔符,如下所示:</p>
<pre><code>col1, col2, col3
col11, col12, col13
col21, col22, col23
col31, col32, col33
</code></pre>
<p>以下代码将对其进行分析并打印每行的第一列和最后一列:</p>
<pre><code># open file to read
f = file('db.txt', 'r')
# iterate over the lines in the file
for line in f:
# split the line into a list of column values
columns = line.split(',')
# clean any whitespace off the items
columns = [col.strip() for col in columns]
# ensure the column has at least one value before printing
if columns:
print "first", columns[0] # print the first column
print "last", columns[-1] # print the last column
</code></pre>