Python-bloggers

Import Simple Files into Python

This article was first published on python – educational research techniques , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In this post we will be using Python to import files. Importing a text file is rather easy into Python. We will look at several different examples and file types in this post.

Importing a Text File

Importing a text file is often done in Python. To do this see the code below.

file=open('Corr.txt',mode='r')
text=file.read()
file.close()
print(text)
$r
              ACmean    CLMean   SFIMean EnrichMean
ACmean     1.0000000 0.4386146 0.2463862  0.5758464
CLMean     0.4386146 1.0000000 0.2874991  0.5730721
SFIMean    0.2463862 0.2874991 1.0000000  0.2076200
EnrichMean 0.5758464 0.5730721 0.2076200  1.0000000

$n
           ACmean CLMean SFIMean EnrichMean
ACmean        172    172     172        172
CLMean        172    172     172        172
SFIMean       172    172     172        172
EnrichMean    172    172     172        172

$P
                 ACmean       CLMean      SFIMean   EnrichMean
ACmean               NA 1.763762e-09 0.0011214521 0.000000e+00
CLMean     1.763762e-09           NA 0.0001312634 2.220446e-16
SFIMean    1.121452e-03 1.312634e-04           NA 6.277935e-03
EnrichMean 0.000000e+00 2.220446e-16 0.0062779348           NA

attr(,"class")
[1] "rcorr"

In order to load the text file we used the open() function to open the file in the working directory. Next, we indicated the mode as ‘r’ which means ‘read’. Everything that was just mentioned was saved into an object called ‘file’. Then we use the read() function on the object called ‘file’ and save all this in a new object called ‘text’. The next step involves using the close() function in order to complete the process. The last step involves printing the content of the text object using print().

Below is a way to complete this process faster.

with open('Corr.txt','r') as file:
    print(file.read())

$r
              ACmean    CLMean   SFIMean EnrichMean
ACmean     1.0000000 0.4386146 0.2463862  0.5758464
CLMean     0.4386146 1.0000000 0.2874991  0.5730721
SFIMean    0.2463862 0.2874991 1.0000000  0.2076200
EnrichMean 0.5758464 0.5730721 0.2076200  1.0000000

$n
           ACmean CLMean SFIMean EnrichMean
ACmean        172    172     172        172
CLMean        172    172     172        172
SFIMean       172    172     172        172
EnrichMean    172    172     172        172

$P
                 ACmean       CLMean      SFIMean   EnrichMean
ACmean               NA 1.763762e-09 0.0011214521 0.000000e+00
CLMean     1.763762e-09           NA 0.0001312634 2.220446e-16
SFIMean    1.121452e-03 1.312634e-04           NA 6.277935e-03
EnrichMean 0.000000e+00 2.220446e-16 0.0062779348           NA

attr(,"class")
[1] "rcorr"
ad

Using the ‘with’ approach is much faster and simpler. THe content of the open() function is the same while we save its as “file” by writing this after the open() function rather than before. Lastly, we print the file and use the read() function together.

Import with Numpy

Naturally, there is more than one way to import data. The example below involve the use of the NumPy library. This approach is used in particular for dealing with numerical data that might be saved as a texr file. Below is an example of how to do this.

import numpy as np
text=np.loadtxt('sample.txt', delimiter=',')
print(text)

[[1. 2. 3. 4.]
 [5. 6. 7. 8.]
 [9. 0. 1. 2.]]

We begin by importing the numpy librar as np. Next, we create an object called ‘text’ and use the loadtxt() function to load a text file called ‘sample’. The argument ‘delimiter’ is used to tell numpy how the numbers are separated in the file. Lastly, we print the ‘text’ object as an array.

Import with Pandas

Pandas is another way to import data. For our example, we will look at how to import csv files. Below is the code for how to complete this.

import pandas as pd
data=pd.read_csv('sample.csv')
data.head()

In line 1 of the code we load the pandas library as ‘pd’. In line 2, we use the read_csv() function to load our data into the ‘data’ object. Lastly, we use head() to take a peek at the first few lines of data.

Conclusion

With this information, you now possess some basic knowledge on how to get data into Python for the purpose of being able to manipulate it. In a future post, we will look at other ways and means of importing data into Python.

To leave a comment for the author, please follow the link and comment on their blog: python – educational research techniques .

Want to share your content on python-bloggers? click here.
Exit mobile version