从存储在行和列中的文件中读取数据

2024-04-24 12:07:22 发布

您现在位置:Python中文网/ 问答频道 /正文

I have a text file with data arranged into rows and columns.I want to
read data as [row][column] value from the file

Example:
1 2 31 4
2 3 41 456
4 5 90 1120
3 4 55 1001
For Example If I need to get the value at first row,third column i.e 31
How can I do so??


Tags: andthetotextdatavalueexamplehave
1条回答
网友
1楼 · 发布于 2024-04-24 12:07:22

使用^{}模块,使用空格作为分隔符。如果适合您的应用程序,则逐行处理,或者将数据读入列表以提供对数据的随机访问。示例:

import csv

with open('file.csv') as f:
    data = [row for row in csv.reader(f, delimiter=' ')]

print(data[0][2])
print(data[3][3])

输出:

31
1001

相关问题 更多 >