Categories
excel file python

read excel file in python

In this tutorial, we will learn about read excel file in python using the pandas library.

In this example, we are using the pandas library to read an excel file.

Pandas is an open-source library that has a lot of features inbuilt. Here we are using it for reading the excel file.
For More refer to official docs of the pandas
https://pandas.pydata.org/
For our exercise, I am going to use the below excel file format.

Pandas Installtion

pip install pandas

Read excel example

# Using pandas library
import pandas as panda


class ReadExcel:
    # Main method
    if __name__ == "__main__":
        # file path of xlsx file
        file_path = "D://data/Avengers.xlsx"
        # reading the excel file
        excel = panda.read_excel(file_path)
        print(excel)

Output

   ID   Character Name           Real Name
0   1             Hulk        Mark Ruffalo
1   2             Thor     Chris Hemsworth
2   3      Black Widow  Scarlett Johansson
3   4         Iron Man    Robert Downey Jr
4   5  Captain America         Chris Evans

References

https://beginnersbug.com/create-dataframe-in-python-using-pandas/

Related Articles

Categories
file java

how to read a file from java with example

In this post, we will learn about how to read a file from java with example

This will called as IO operations

There are many ways to read a file from java. BufferedReader, Scanner, Streams

BufferReader

BufferReader is one of the common way to read a file from java. It will read the file line by line

It is synchronized, so the operations will be thread safe

Once the read operation is done we need to close the BufferReader

we are getting chance of IOException in this code, so we should surround the code by try catch

Example

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class ReadFileExample {

	public static void main(String[] args) {
		try {
			
			File file = new File("C://temp/sample.txt");
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
			
			// Converting input stream to buffer reader
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String temp = "";
			StringBuffer textFileContent = new StringBuffer();
			while ((temp = bufferedReader.readLine()) != null) {
				textFileContent.append(temp);
			}
			
			System.out.println(textFileContent);
			// In below line we are closing the bufferReader
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
Output
sdfdsfdsfsdfsHelloworld
Scanner

In this example we will do the same operation using Scanner. Here are also we used while loop to iterate the content of file as like bufferedReader

Once we done with the operation we nee to close the scanner as like BufferReader

Scanner Example

import java.io.File;
import java.util.Scanner;

public class ReadFileScannerExample {

	public static void main(String[] args) {
		try {
			File file = new File("C:\\temp\\sample.txt");
			
			Scanner scanner = new Scanner(file);
			StringBuffer textFileContent = new StringBuffer();
			while (scanner.hasNextLine()) {
				textFileContent.append(scanner.nextLine());
			}
			System.out.println(textFileContent);
			scanner.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Output
sdfdsfdsfsdfsHelloworld
Using NIO pacakge

Using NIO package we have Files.ReadAllBytes method. Using this we can read file as byte array. Then we can convert this into a string as like below example

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Sample {

	public static void main(String[] args) throws IOException {
		byte[] encoded = Files.readAllBytes(Paths.get("D:\\workspace\\sample.txt"));
		String s = new String(encoded, StandardCharsets.US_ASCII);
		System.out.println(s);
	}
}
java.io.FileNotFoundException

If java not able to find the file it will throw the below exception

java.io.FileNotFoundException: C:\temp\sample.txt1 (The system cannot find the file specified)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.util.Scanner.<init>(Scanner.java:611)
	at com.geeks.example.ReadFileScannerExample.main(ReadFileScannerExample.java:12)
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/files/ReadFileExample.java

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/files/ReadFileScannerExample.java

Related Articles

create text file from java with example