Read raw file python

Python File I/O — Read and Write Files

In Python, the IO module provides methods of three types of IO operations; raw binary files, buffered binary files, and text files. The canonical way to create a file object is by using the open() function.

Any file operations can be performed in the following three steps:

  1. Open the file to get the file object using the built-in open() function. There are different access modes, which you can specify while opening a file using the open() function.
  2. Perform read, write, append operations using the file object retrieved from the open() function.
  3. Close and dispose the file object.

Reading File

File object includes the following methods to read data from the file.

  • read(chars): reads the specified number of characters starting from the current position.
  • readline(): reads the characters starting from the current reading position up to a newline character.
  • readlines(): reads all lines until the end of file and returns a list object.

The following C:\myfile.txt file will be used in all the examples of reading and writing files.

This is the first line. This is the second line. This is the third line. 

The following example performs the read operation using the read(chars) method.

f = open('C:\myfile.txt') # opening a file lines = f.read() # reading a file print(lines) #'This is the first line. \nThis is the second line.\nThis is the third line.' f.close() # closing file object 

Above, f = open(‘C:\myfile.txt’) opens the myfile.txt in the default read mode from the current directory and returns a file object. f.read() function reads all the content until EOF as a string. If you specify the char size argument in the read(chars) method, then it will read that many chars only. f.close() will flush and close the stream.

Reading a Line

The following example demonstrates reading a line from the file.

f = open('C:\myfile.txt') # opening a file line1 = f.readline() # reading a line print(line1) #'This is the first line. \n' line2 = f.readline() # reading a line print(line2) #'This is the second line.\n' line3 = f.readline() # reading a line print(line3) #'This is the third line.' line4 = f.readline() # reading a line print(line4) #'' f.close() # closing file object 

As you can see, we have to open the file in ‘r’ mode. The readline() method will return the first line, and then will point to the second line in the file.

Читайте также:  Java operations on collections

Reading All Lines

The following reads all lines using the readlines() function.

f = open('C:\myfile.txt') # opening a file lines = f.readlines() # reading all lines print(lines) #'This is the first line. \nThis is the second line.\nThis is the third line.' f.close() # closing file object 

The file object has an inbuilt iterator. The following program reads the given file line by line until StopIteration is raised, i.e., the EOF is reached.

f=open('C:\myfile.txt') while True: try: line=next(f) print(line) except StopIteration: break f.close() 

Use the for loop to read a file easily.

f=open('C:\myfile.txt') for line in f: print(line) f.close() 
This is the first line. This is the second line. This is the third line. 

Reading Binary File

Use the ‘rb’ mode in the open() function to read a binary files, as shown below.

f = open('C:\myimg.png', 'rb') # opening a binary file content = f.read() # reading all lines print(content) #print content f.close() # closing file object 

Writing to a File

The file object provides the following methods to write to a file.

  • write(s): Write the string s to the stream and return the number of characters written.
  • writelines(lines): Write a list of lines to the stream. Each line must have a separator at the end of it.

Create a new File and Write

The following creates a new file if it does not exist or overwrites to an existing file.

f = open('C:\myfile.txt','w') f.write("Hello") # writing to file f.close() # reading file f = open('C:\myfile.txt','r') f.read() #'Hello' f.close() 

In the above example, the f=open(«myfile.txt»,»w») statement opens myfile.txt in write mode, the open() method returns the file object and assigns it to a variable f . ‘w’ specifies that the file should be writable. Next, f.write(«Hello») overwrites an existing content of the myfile.txt file. It returns the number of characters written to a file, which is 5 in the above example. In the end, f.close() closes the file object.

Appending to an Existing File

The following appends the content at the end of the existing file by passing ‘a’ or ‘a+’ mode in the open() method.

f = open('C:\myfile.txt','a') f.write(" World!") f.close() # reading file f = open('C:\myfile.txt','r') f.read() #'Hello World!' f.close() 

Write Multiple Lines

Python provides the writelines() method to save the contents of a list object in a file. Since the newline character is not automatically written to the file, it must be provided as a part of the string.

lines=["Hello world.\n", "Welcome to TutorialsTeacher.\n"] f=open("D:\myfile.txt", "w") f.writelines(lines) f.close() 

Opening a file with «w» mode or «a» mode can only be written into and cannot be read from. Similarly «r» mode allows reading only and not writing. In order to perform simultaneous read/append operations, use «a+» mode.

Читайте также:  Меню по горизонтали css

Writing to a Binary File

The open() function opens a file in text format by default. To open a file in binary format, add ‘b’ to the mode parameter. Hence the «rb» mode opens the file in binary format for reading, while the «wb» mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable. When opened using any text editor, the data is unrecognizable.

The following code stores a list of numbers in a binary file. The list is first converted in a byte array before writing. The built-in function bytearray() returns a byte representation of the object.

f=open("binfile.bin","wb") num=[5, 10, 15, 20, 25] arr=bytearray(num) f.write(arr) f.close() 

Источник

Reading Raw Files¶

The RawRead class is used to (surprise…) read .RAW-files. The file to read is given as parameter when creating the RawRead object. The object wil read the file and construct a structure of objects which can be used to access the data inside the .RAW-file. All traces on the .RAW-file are uploaded into memory.

See RAW File Structure for details of the contents of a .RAW-file.

The .RAW-file contains different traces for voltages and currents in the simulation.

Typically (for a transient analysis), a trace contain a value (voltage/current) for each different time point in the simulation. There is a list of time values, and a separate list of trace values for each trace. So each trace uses the same time values. If there were different steps (e.g. for a DC sweep), then there is a set of lists with time/value data for each step.

Note that for an AC analysis, the traces are frequency-versus-value instead of time-versus-value. We will use ‘time’ as an example further in this text.

The RawRead class has all the methods that allow the user to access the X-axis and trace values. If there is any stepped data (.STEP primitives), the RawRead class will try to load the log information from the same directory as the raw file in order to obtain the STEP information.

You can get a list of all trace names using the get_trace_names() method.

Читайте также:  Configuration object in javascript

Use method get_trace() to get the trace data, which consists of values for 1 or more simulation steps. It will return a PyLTSpice.raw_classes.Trace object. Use this object’s get_wave() method to get the actual data points for a step.

Use the method get_axis() to get the ‘time’ data. If there were multiple steps in the simulation, specify the number for which step you want to retrieve the time data.

Now that you have lists with the times and corresponding values, you can plot this information in an X/Y plot.

Note that all the data will be returned as numpy arrays.

See the class documentation for more details :

Example¶

The example below demonstrates the usage of the RawRead class. It reads a .RAW file and uses the matplotlib library to plot the results of two traces in a separate subplots.

from PyLTSpice import RawRead import matplotlib.pyplot as plt # use matplotlib for plotting the results raw = RawRead("some_random_file.raw") # Read the RAW file contents from disk print(raw.get_trace_names()) # Get and print a list of all the traces print(raw.get_raw_property()) # Print all the properties found in the Header section vin = raw.get_trace('V(in)') # Get the trace data vout = raw.get_trace('V(out)') # Get the second trace steps = raw.get_steps() # Get list of step numbers ([0,1,2]) for sweeped simulations # Returns [0] if there is just 1 step plt.figure() # Create the canvas for plotting _, (ax1, ax2) = plt.subplots(2, 1, sharex=True) # Create two subplots for ax in (ax1, ax2): # Use grid on both subplots ax.grid(True) plt.xlim([0.9e-3, 1.2e-3]) # Limit the X axis to just a subrange xdata = raw.get_axis() # Get the X-axis data (time) ydata = vin.get_wave() # Get all the values for the 'vin' trace ax1.plot(xdata, ydata) # Do an X/Y plot on first subplot ydata = vout.get_wave() # Get all the values for the 'vout' trace ax1.plot(xdata, ydata) # Do an X/Y plot on first subplot as well for step in steps: # On the second plot, print all the STEPS of Vout ydata = vout.get_wave(step) # Retrieve the values for this step xdata = raw.get_axis(step) # Retrieve the time vector ax2.plot(xdata, ydata) # Do X/Y plot on second subplot plt.show() # Show matplotlib's interactive window with the plots 

Источник

Оцените статью