Python Forum
Searching a .txt file for a specific number and extracting the corresponding data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Searching a .txt file for a specific number and extracting the corresponding data (/thread-11810.html)



Searching a .txt file for a specific number and extracting the corresponding data - nrozman - Jul-26-2018

Hi, I am a beginner at coding and am using anaconda/jupyter notebook to code in python.

I am hoping to create a graph from a large dataset. My dataset has two columns (frequency and transmission).

I want my code to search the first column for all values of 0.6 and then pull the number from the second row(transmission) and store it an array. Aka I want all transmission values for f=0.6 in its own array.

From there I can figure out how to plot that new array of transmission values against an array I have created.

If anyon can help me do this I would really appreciate it!


RE: Searching a .txt file for a specific number and extracting the corresponding data - Vysero - Jul-26-2018

Can you show the code you have so far?


RE: Searching a .txt file for a specific number and extracting the corresponding data - perfringo - Jul-26-2018

Do you mean something like that:

>>> d = [
...     [1, 10],
...     [0.6, 20],
...     [2, 30],
...     [0.6, 40]
...     ]
>>> [row[1] for row in d if row[0] == 0.6]
[20, 40]



RE: Searching a .txt file for a specific number and extracting the corresponding data - nrozman - Jul-27-2018

(Jul-26-2018, 08:34 PM)Vysero Wrote: Can you show the code you have so far?
import numpy as np
import matplotlib.pyplot as plt

data_file1 = np.loadtxt('untitled.txt',delimiter='\t')     
p = data_file1[:,][:,0]              
t = data_file1[:,][:,1]      

plt.figure(1)
plt.rcParams['figure.figsize'] = [50, 50]
plt.plot(p, t,'c')
plt.xlabel('Periodicity [microns]')
plt.ylabel('Transmission')
plt.savefig('XRectangle.png')        # Saves the plot