I got problem to get values from list.
I got opad1.txt file which looks like this:
4.6 2.3 1.9 0.4 0.2 6.8 0.4 0.0 0.1 5.5
0.0 0.4 3.5 0.3 2.3 0.1 0.0 0.0 4.0 1.5
1.5 0.7 0.7 4.9 4.2 1.3 2.7 3.9 6.5 1.2
0.2
I'm using code to read this file and convert each line to list
with open('opad1.txt') as f:
opady = f.readlines()
dekada = [i.split() for i in opady]
And now i got problem to separate each value from list dekada. When i use:
print(dekada[0])
The return is:
['4.6', '2.3', '1.9', '0.4', '0.2', '6.8', '0.4', '0.0', '0.1', '5.5']
My question is how to reach first element only '4.6' ?
if you want to have the content of your text file in a flat manner, you can do something like this:
with open('opad1.txt') as f:
opady = f.readlines()
dekada = str.join(" ", opady).split()
print(dekada[0])
As the comment suggests, use another level to retrieve just a single item, as the first level just equals a whole list (I added the 'r' as well, for read-mode):
with open('opad1.txt', 'r') as f:
opady = f.readlines()
dekada = [i.split() for i in opady]
print(dekada[0][0])
Outputs:
4.6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With