I have a text file with data arranged into rows and columns.I want to
read data as [row][column] value from the fileExample:
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??
Asked
Active
Viewed 278 times
0

avinash
- 155
- 1
- 2
- 8
-
Do you know how to open a file and how to read a line? – Jon Clements Oct 21 '17 at 13:46
-
No i don't know – avinash Oct 21 '17 at 13:47
-
1You might want to pick up a tutorial (there's some listed [here](https://sopython.com/wiki/What_tutorial_should_I_read%3F)) – Jon Clements Oct 21 '17 at 13:49
-
Welcome to SO. Unfortunately this isn't a tutorial or code writing service. Please take the time to read [ask] and the links it contains. You should spend some time working your way through [the Tutorial](https://docs.python.org/3/tutorial/index.html), practicing the examples. There are many other good online resources. – wwii Oct 21 '17 at 13:51
1 Answers
1
Use the csv
module using space as the delimiter. Process row-by-row if that suits your application, or read the data into a list of lists to provide random access to the data. Example:
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])
Output:
31 1001

mhawke
- 84,695
- 9
- 117
- 138
-
-
CSV is a text file with the data formatted as CSV. If you mean some arbitrary text file with some other format then, no, it won't work. – mhawke Oct 21 '17 at 22:38