Python Forum

Full Version: Searching a .txt file for a specific number and extracting the corresponding data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
Can you show the code you have so far?
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]
(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