cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to remove extra ENTER line in csv UTF-16 while reading

shamly
New Contributor III

Dear Friends,

I have a csv and it looks like this

โ€กโ€กIdโ€กโ€ก,โ€กโ€กVersionโ€กโ€ก,โ€กโ€กQuestionnaireโ€กโ€ก,โ€กโ€กDateโ€กโ€ก

โ€กโ€ก123456โ€กโ€ก,โ€กโ€กVersion2โ€กโ€ก,โ€กโ€กAll questions have been answered accurately

and the guidance in the questionnaire was understood and followedโ€กโ€ก,โ€กโ€ก2010-12-16 00:01:48.020000000โ€กโ€ก

There is an extra ENTER line "and the guidance in the questionnaire was understood and followed" this part is coming as a new line in the csv.  Source file encoding is UTF-16 LE BOM.

At the end of every line, I have CRLF and at the end of every ENTER extra line, I have LF

I should mention in my code something like lineSep \r\n ,but how?

I wrote below code to read this csv

dff = spark.read.option("header", "true") \

.option("inferSchema", "true") \

.option('multiline', 'true') \

.option('encoding', 'UTF-16') \

.option("delimiter", "โ€กโ€ก,โ€กโ€ก") \

.csv("/mnt/path/data.csv")

dffs_headers = dff.dtypes

for i in dffs_headers:

  columnLabel = i[0]

  newColumnLabel = columnLabel.replace('โ€กโ€ก','').replace('โ€กโ€ก','')

  dff=dff.withColumn(newColumnLabel,regexp_replace(columnLabel,'^\\โ€กโ€ก|\\โ€กโ€ก$',''))

  if columnLabel != newColumnLabel:

    dff = dff.drop(columnLabel)

    display(dff)

But, in the result is not correct as for the given Id, Questionnaire column data is breaking after "All questions have been answered accurately" and displayed in the next row. I want the entire textbetween the doubledagger "โ€กโ€ก,โ€กโ€ก" to be read as one row, even if there is any extra ENTER line.

Please help friends @Aviral Bhardwajโ€‹ 

@DataBricksHelp232โ€‹ @Rahul@Databricksโ€‹ @Uma Dacharlaโ€‹ @Uma Maheswara Rao Desulaโ€‹ 

3 REPLIES 3

Aviral-Bhardwaj
Esteemed Contributor III

This is working fine,

image 

from pyspark.sql.functions import regexp_replace
 
path="dbfs:/FileStore/df/test.csv"
dff = spark.read.option("header", "true").option("inferSchema", "true").option('multiline', 'true').option('encoding', 'UTF-8').option("delimiter", "โ€กโ€ก,โ€กโ€ก").csv(path)
 
for i in dffs_headers:
  columnLabel = i[0]
  newColumnLabel = columnLabel.replace('โ€กโ€ก','').replace('โ€กโ€ก','')
  
  dff=dff.withColumn(newColumnLabel,regexp_replace(columnLabel,'^\\โ€กโ€ก|\\โ€กโ€ก$',''))
  
  if columnLabel != newColumnLabel:
    dff = dff.drop(columnLabel)
  dff.show(truncate=False)

Please select my answer as the best answer it will be a great help

Thanks

Aviral Bhardwaj

AviralBhardwaj

Hi,

This is not working for me as the source file encoding is UTF-16 LE BOM.

At the end of every line, I have CRLF and at the end of every ENTER extra line,I have LF

I should mention in my code something like lineSep \r\n ,but how?

Aviral-Bhardwaj
Esteemed Contributor III

connect with me here - https://www.linkedin.com/in/aviralb/

We will try to solve in live call

AviralBhardwaj

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group