how to read and write special characters using pyspark

Erabathini, Rajendar 20 Reputation points
2023-03-18T19:02:18.94+00:00

HI - I have a file with pipe ( | ) delimiter values. However they have special characters like comma( , ) and double qutoes ( " ) in some columns. I am writing the data to another file using dataframe but these characters are not written there instead '/' and spaces are inserted in place of them. Please suggest how can I write them as it is to another file using pyspark in synapse analytics notebook.

thanks,
RR Netha

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,378 questions
{count} votes

Accepted answer
  1. AnnuKumari-MSFT 34,556 Reputation points Microsoft Employee Moderator
    2023-03-21T13:46:55.03+00:00

    Hi @Erabathini, Rajendar ,

    Welcome to Microsoft Q&A platform and thanks for posting your question here.

    As I understand your question, you are trying to write data from one csv file having pipe delimiter and symbols (i.e. comma and double quotes) into another csv file. However, in the target file, symbols are not retained and they are getting replaced by slash '/'. Please let me know if that is not the case.

    '/' before " is expected in the target file if you have not explicitly defined quote other than double quote ("). Since delimited text files by default enclose string data with double quotes(eg: "ABC","india"), so in order to escape the " which lies in the data , it puts '/' automatically . So, Kindly make sure to include quote parameter having value anything other than double quote. You can use single quote in quote parameter like below :

    df.write.csv('abfss://<file_system>@<account_name>.dfs.core.windows.net/<path>/<file_name>', quote="'")
    
    

    User's image

    Without quote param:User's image

    With quote param:User's image

    Hope it helps. Please do consider clicking Accept Answer as accepted answers help community as well. Also, please click on Yes for the survey 'Was the answer helpful'

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.