Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to Read CSV Files in PySpark but it is also reading Text Files

I have a folder having .txt and .csv files (having exactly same column names)

However, while I am trying to read only CSV Files in PySpark and trying the following code below it is reading and appending both text and csv files together

from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("CSV Reader").getOrCreate()

csv_path = "path/to/csv/folder"

df = spark.read \
.format("csv") \
.option("header", "true") \
.option("inferSchema", "true") \
.load(csv_path)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use pathGlobFilter as an option and define a pattern to read only .csv files

spark.read.format("csv").option('pathGlobFilter', '*.csv').load(csv_path)

Hope this is going to help
I’ve found that option here: https://dbmstutorials.com/pyspark/spark-read-write-dataframe-options.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading