使用python读取txt最后一列,并打印
时间: 2023-05-15 17:05:37 浏览: 216
可以使用以下代码读取txt文件的最后一列并打印出来:
```python
with open('file.txt', 'r') as f:
lines = f.readlines()
last_column = [line.split()[-1] for line in lines]
print(last_column)
```
其中,'file.txt'是你要读取的txt文件名。这段代码会将文件的每一行读取出来,然后取每一行的最后一列,最后将所有最后一列的数据打印出来。
相关问题
使用python读取txt中按照条件查找的最后一列,并打印
可以使用以下代码实现:
```python
with open('file.txt', 'r') as f:
for line in f:
if condition:
last_column = line.split()[-1]
print(last_column)
```
其中,'file.txt'是要读取的文件名,condition是按照什么条件查找最后一列的条件。这段代码会逐行读取文件,如果满足条件,则将该行最后一列赋值给last_column变量,并最终打印出来。
python 读取txt的特定一列
可以使用Python的pandas库来读取txt文件的特定一列。以下是示例代码:
```python
import pandas as pd
# 读取txt文件
df = pd.read_csv('file.txt', sep='\t', header=None)
# 获取特定一列
col = df[0]
# 打印结果
print(col)
```
其中,`read_csv`函数用于读取txt文件,`sep='\t'`表示使用制表符作为分隔符,`header=None`表示文件中没有列名。`df[0]`表示获取第一列数据,可以根据需要修改为其他列。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)