You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Saurabh Gulati <sa...@fedex.com.INVALID> on 2023/01/04 10:30:06 UTC
Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
Hey guys, much appreciate your quick responses.
To answer your questions,
@Mich Talebzadeh<ma...@gmail.com> We get data from multiple sources, and we don't have any control over what they put in. In this case the column is supposed to contain some feedback and it can also contain quoted strings.
@Sean Owen<ma...@gmail.com> Also see the example below with quotes feedback:
"a","b","c"
"1","",",see what ""I did"","
"2","","abc"
Here if we don't escape with "
df = spark.read.option("multiLine", True).option("enforceSchema", False).option("header", True).csv(f"/tmp/test.csv")
df.show(100, False)
+---+----+--------------------+
|a |b |c |
+---+----+--------------------+
|1 |null|",see what ""I did""|
+---+----+--------------------+
df.count()
1
So, we put in "? as the escape character and then its parsed fine but the count is wrong.
df = spark.read.option("escape", '"').option("multiLine", True).option("enforceSchema", False).option("header", True).csv(f"/tmp/test.csv")
df.show(100, False)
+---+----+------------------+
|a |b |c |
+---+----+------------------+
|1 |null|,see what "I did",|
|2 |null|abc |
+---+----+------------------+
df.count()
1
I understand its a complex case or maybe an edge case which makes it difficult for spark
to understand when a column ends as we have even enabled multiline=True?.
See another example below which even has multiline value for column c?.
"a","b","c"
"1","",",see what ""I did"",
i am still writing"
"2","","abc"
# with escape
df = spark.read.option("escape", '"').option("multiLine", True).option("enforceSchema", False).option("header", True).csv(f"/tmp/test.csv")
df.show(10, False)
+---+----+--------------------------------------+
|a |b |c |
+---+----+--------------------------------------+
|1 |null|,see what "I did",\ni am still writing|
|2 |null|abc |
+---+----+--------------------------------------+
df.count()
1
df.select("c").show(10, False)
+------------------+
|c |
+------------------+
|see what ""I did""|
|null |
|abc |
+------------------+
# without escape "
df.show(10, False)
+-------------------+----+--------------------+
|a |b |c |
+-------------------+----+--------------------+
|1 |null|",see what ""I did""|
|i am still writing"|null|null |
|2 |null|abc |
+-------------------+----+--------------------+
df.select("c").show(10, False)
+--------------------+
|c |
+--------------------+
|",see what ""I did""|
|null |
|abc |
+--------------------+
The issue is that it can print the complete data frame correctly with escape enabled,
but when you select a column or ask a count then it gives wrong output.
Regards
Saurabh
________________________________
From: Mich Talebzadeh <mi...@gmail.com>
Sent: 04 January 2023 10:14
To: Sean Owen <sr...@gmail.com>
Cc: Saurabh Gulati <sa...@fedex.com>; User <us...@spark.apache.org>
Subject: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
Caution! This email originated outside of FedEx. Please do not open attachments or click links from an unknown or suspicious origin.
What is the point of having , as a column value? From a business point of view it does not signify anything IMO
[https://ci3.googleusercontent.com/mail-sig/AIorK4zholKucR2Q9yMrKbHNn-o1TuS4mYXyi2KO6Xmx6ikHPySa9MLaLZ8t2hrA6AUcxSxDgHIwmKE] view my Linkedin profile<https://urldefense.com/v3/__https://www.linkedin.com/in/mich-talebzadeh-ph-d-5205b2/__;!!BL9GA0TyTA!erAOVE6gcxktT3dQCu6OSdzFqng9xRG1oLmXuetC6pn_3nMnzlWnC_pNmhtZMwXPc3QxaSb8w6V55rIjuRXHqVXSIPo5aQ$>
https://en.everybodywiki.com/Mich_Talebzadeh<https://urldefense.com/v3/__https://en.everybodywiki.com/Mich_Talebzadeh__;!!BL9GA0TyTA!erAOVE6gcxktT3dQCu6OSdzFqng9xRG1oLmXuetC6pn_3nMnzlWnC_pNmhtZMwXPc3QxaSb8w6V55rIjuRXHqVUcys0piQ$>
Disclaimer: Use it at your own risk. Any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on this email's technical content is explicitly disclaimed. The author will in no case be liable for any monetary damages arising from such loss, damage or destruction.
On Tue, 3 Jan 2023 at 20:39, Sean Owen <sr...@gmail.com>> wrote:
Why does the data even need cleaning? That's all perfectly correct. The error was setting quote to be an escape char.
On Tue, Jan 3, 2023, 2:32 PM Mich Talebzadeh <mi...@gmail.com>> wrote:
if you take your source CSV as below
"a","b","c"
"1","",","
"2","","abc"
and define your code as below
csv_file="hdfs://rhes75:9000/data/stg/test/testcsv.csv"
# read hive table in spark
listing_df = spark.read.format("com.databricks.spark.csv").option("inferSchema", "true").option("header", "true").load(csv_file)
listing_df.printSchema()
print(f"""\n Reading from Hive table {csv_file}\n""")
listing_df.show(100,False)
listing_df.select("c").show()
results in
Reading from Hive table hdfs://rhes75:9000/data/stg/test/testcsv.csv
+---+----+---+
|a |b |c |
+---+----+---+
|1 |null|, |
|2 |null|abc|
+---+----+---+
+---+
| c|
+---+
| ,|
|abc|
+---+
which assumes that "," is a value for column c in row 1
This interpretation is correct. You ought to do data cleansing before.
HTH
[https://ci3.googleusercontent.com/mail-sig/AIorK4zholKucR2Q9yMrKbHNn-o1TuS4mYXyi2KO6Xmx6ikHPySa9MLaLZ8t2hrA6AUcxSxDgHIwmKE] view my Linkedin profile<https://urldefense.com/v3/__https://www.linkedin.com/in/mich-talebzadeh-ph-d-5205b2/__;!!BL9GA0TyTA!erAOVE6gcxktT3dQCu6OSdzFqng9xRG1oLmXuetC6pn_3nMnzlWnC_pNmhtZMwXPc3QxaSb8w6V55rIjuRXHqVXSIPo5aQ$>
https://en.everybodywiki.com/Mich_Talebzadeh<https://urldefense.com/v3/__https://en.everybodywiki.com/Mich_Talebzadeh__;!!BL9GA0TyTA!erAOVE6gcxktT3dQCu6OSdzFqng9xRG1oLmXuetC6pn_3nMnzlWnC_pNmhtZMwXPc3QxaSb8w6V55rIjuRXHqVUcys0piQ$>
Disclaimer: Use it at your own risk. Any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on this email's technical content is explicitly disclaimed. The author will in no case be liable for any monetary damages arising from such loss, damage or destruction.
On Tue, 3 Jan 2023 at 17:03, Sean Owen <sr...@gmail.com>> wrote:
No, you've set the escape character to double-quote, when it looks like you mean for it to be the quote character (which it already is). Remove this setting, as it's incorrect.
On Tue, Jan 3, 2023 at 11:00 AM Saurabh Gulati <sa...@fedex.com.invalid> wrote:
Hello,
We are seeing a case with csv data when it parses csv data incorrectly.
The issue can be replicated using the below csv data
"a","b","c"
"1","",","
"2","","abc"
and using the spark csv read command.
df = spark.read.format("csv")\
.option("multiLine", True)\
.option("escape", '"')\
.option("enforceSchema", False) \
.option("header", True)\
.load(f"/tmp/test.csv")
df.show(100, False) # prints both rows
|a |b |c |
+---+--------+---+
|1 |null |, |
|2 |null |abc|
df.select("c").show() # merges last column of first row and first column of second row
+------+
| c|
+------+
|"\n"2"|
print(df.count()) # prints 1, should be 2
It feels like a bug and I thought of asking the community before creating a bug on jira.
Mvg/Regards
Saurabh
Re: [EXTERNAL] Re: Re: Incorrect csv parsing when delimiter used within the data
Posted by Saurabh Gulati <sa...@fedex.com.INVALID>.
Yes, there are other ways to solve this but trying to understand why there is a difference in behaviour between df.show() and df.select("c").show()
Mvg/Regards
Saurabh Gulati
________________________________
From: Shay Elbaz <sh...@gm.com>
Sent: 04 January 2023 14:54
To: Saurabh Gulati <sa...@fedex.com.INVALID>; Sean Owen <sr...@gmail.com>
Cc: Mich Talebzadeh <mi...@gmail.com>; User <us...@spark.apache.org>
Subject: Re: [EXTERNAL] Re: Re: Incorrect csv parsing when delimiter used within the data
If you have found a parser that works, simply read the data as text files, apply the parser manually, and convert to DataFrame (if needed at all),
________________________________
From: Saurabh Gulati <sa...@fedex.com.INVALID>
Sent: Wednesday, January 4, 2023 3:45 PM
To: Sean Owen <sr...@gmail.com>
Cc: Mich Talebzadeh <mi...@gmail.com>; User <us...@spark.apache.org>
Subject: [EXTERNAL] Re: Re: Incorrect csv parsing when delimiter used within the data
ATTENTION: This email originated from outside of GM.
Hi @Sean Owen<ma...@gmail.com>
Probably the data is incorrect, and the source needs to fix it.
But using python's csv parser returns the correct results.
import csv
with open("/tmp/test.csv") as c_file:
csv_reader = csv.reader(c_file, delimiter=",")
for row in csv_reader:
print(row)
['a', 'b', 'c']
['1', '', ',see what "I did",\ni am still writing']
['2', '', 'abc']
And also, I don't understand why there is a distinction in outputs from df.show() and df.select("c").show()
Mvg/Regards
Saurabh Gulati
Data Platform
________________________________
From: Sean Owen <sr...@gmail.com>
Sent: 04 January 2023 14:25
To: Saurabh Gulati <sa...@fedex.com>
Cc: Mich Talebzadeh <mi...@gmail.com>; User <us...@spark.apache.org>
Subject: Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
That input is just invalid as CSV for any parser. You end a quoted col without following with a col separator. What would the intended parsing be and how would it work?
On Wed, Jan 4, 2023 at 4:30 AM Saurabh Gulati <sa...@fedex.com>> wrote:
@Sean Owen<ma...@gmail.com> Also see the example below with quotes feedback:
"a","b","c"
"1","",",see what ""I did"","
"2","","abc"
Re: [EXTERNAL] Re: Re: Incorrect csv parsing when delimiter used within the data
Posted by Shay Elbaz <sh...@gm.com>.
If you have found a parser that works, simply read the data as text files, apply the parser manually, and convert to DataFrame (if needed at all),
________________________________
From: Saurabh Gulati <sa...@fedex.com.INVALID>
Sent: Wednesday, January 4, 2023 3:45 PM
To: Sean Owen <sr...@gmail.com>
Cc: Mich Talebzadeh <mi...@gmail.com>; User <us...@spark.apache.org>
Subject: [EXTERNAL] Re: Re: Incorrect csv parsing when delimiter used within the data
ATTENTION: This email originated from outside of GM.
Hi @Sean Owen<ma...@gmail.com>
Probably the data is incorrect, and the source needs to fix it.
But using python's csv parser returns the correct results.
import csv
with open("/tmp/test.csv") as c_file:
csv_reader = csv.reader(c_file, delimiter=",")
for row in csv_reader:
print(row)
['a', 'b', 'c']
['1', '', ',see what "I did",\ni am still writing']
['2', '', 'abc']
And also, I don't understand why there is a distinction in outputs from df.show() and df.select("c").show()
Mvg/Regards
Saurabh Gulati
Data Platform
________________________________
From: Sean Owen <sr...@gmail.com>
Sent: 04 January 2023 14:25
To: Saurabh Gulati <sa...@fedex.com>
Cc: Mich Talebzadeh <mi...@gmail.com>; User <us...@spark.apache.org>
Subject: Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
That input is just invalid as CSV for any parser. You end a quoted col without following with a col separator. What would the intended parsing be and how would it work?
On Wed, Jan 4, 2023 at 4:30 AM Saurabh Gulati <sa...@fedex.com>> wrote:
@Sean Owen<ma...@gmail.com> Also see the example below with quotes feedback:
"a","b","c"
"1","",",see what ""I did"","
"2","","abc"
Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
Posted by Saurabh Gulati <sa...@fedex.com.INVALID>.
and 2 single quotes together'' are looking like a single double quote ".
Mvg/Regards
Saurabh Gulati
________________________________
From: Saurabh Gulati <sa...@fedex.com>
Sent: 05 January 2023 12:24
To: Sean Owen <sr...@gmail.com>
Cc: User <us...@spark.apache.org>
Subject: Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
Its the same input except that headers are also being read with csv reader.
Mvg/Regards
Saurabh Gulati
________________________________
From: Sean Owen <sr...@gmail.com>
Sent: 04 January 2023 15:12
To: Saurabh Gulati <sa...@fedex.com>
Cc: User <us...@spark.apache.org>
Subject: Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
That does not appear to be the same input you used in your example. What is the contents of test.csv?
On Wed, Jan 4, 2023 at 7:45 AM Saurabh Gulati <sa...@fedex.com>> wrote:
Hi @Sean Owen<ma...@gmail.com>
Probably the data is incorrect, and the source needs to fix it.
But using python's csv parser returns the correct results.
import csv
with open("/tmp/test.csv") as c_file:
csv_reader = csv.reader(c_file, delimiter=",")
for row in csv_reader:
print(row)
['a', 'b', 'c']
['1', '', ',see what "I did",\ni am still writing']
['2', '', 'abc']
And also, I don't understand why there is a distinction in outputs from df.show() and df.select("c").show()
Mvg/Regards
Saurabh Gulati
Data Platform
________________________________
From: Sean Owen <sr...@gmail.com>>
Sent: 04 January 2023 14:25
To: Saurabh Gulati <sa...@fedex.com>>
Cc: Mich Talebzadeh <mi...@gmail.com>>; User <us...@spark.apache.org>>
Subject: Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
That input is just invalid as CSV for any parser. You end a quoted col without following with a col separator. What would the intended parsing be and how would it work?
On Wed, Jan 4, 2023 at 4:30 AM Saurabh Gulati <sa...@fedex.com>> wrote:
@Sean Owen<ma...@gmail.com> Also see the example below with quotes feedback:
"a","b","c"
"1","",",see what ""I did"","
"2","","abc"
Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
Posted by Saurabh Gulati <sa...@fedex.com.INVALID>.
Its the same input except that headers are also being read with csv reader.
Mvg/Regards
Saurabh Gulati
________________________________
From: Sean Owen <sr...@gmail.com>
Sent: 04 January 2023 15:12
To: Saurabh Gulati <sa...@fedex.com>
Cc: User <us...@spark.apache.org>
Subject: Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
That does not appear to be the same input you used in your example. What is the contents of test.csv?
On Wed, Jan 4, 2023 at 7:45 AM Saurabh Gulati <sa...@fedex.com>> wrote:
Hi @Sean Owen<ma...@gmail.com>
Probably the data is incorrect, and the source needs to fix it.
But using python's csv parser returns the correct results.
import csv
with open("/tmp/test.csv") as c_file:
csv_reader = csv.reader(c_file, delimiter=",")
for row in csv_reader:
print(row)
['a', 'b', 'c']
['1', '', ',see what "I did",\ni am still writing']
['2', '', 'abc']
And also, I don't understand why there is a distinction in outputs from df.show() and df.select("c").show()
Mvg/Regards
Saurabh Gulati
Data Platform
________________________________
From: Sean Owen <sr...@gmail.com>>
Sent: 04 January 2023 14:25
To: Saurabh Gulati <sa...@fedex.com>>
Cc: Mich Talebzadeh <mi...@gmail.com>>; User <us...@spark.apache.org>>
Subject: Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
That input is just invalid as CSV for any parser. You end a quoted col without following with a col separator. What would the intended parsing be and how would it work?
On Wed, Jan 4, 2023 at 4:30 AM Saurabh Gulati <sa...@fedex.com>> wrote:
@Sean Owen<ma...@gmail.com> Also see the example below with quotes feedback:
"a","b","c"
"1","",",see what ""I did"","
"2","","abc"
Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
Posted by Sean Owen <sr...@gmail.com>.
That does not appear to be the same input you used in your example. What is
the contents of test.csv?
On Wed, Jan 4, 2023 at 7:45 AM Saurabh Gulati <sa...@fedex.com>
wrote:
> Hi @Sean Owen <sr...@gmail.com>
> Probably the data is incorrect, and the source needs to fix it.
> But using python's csv parser returns the correct results.
>
> import csv
>
> with open("/tmp/test.csv") as c_file:
>
> csv_reader = csv.reader(c_file, delimiter=",")
> for row in csv_reader:
> print(row)
>
> ['a', 'b', 'c']
> ['1', '', ',see what "I did",\ni am still writing']
> ['2', '', 'abc']
>
> And also, I don't understand why there is a distinction in outputs from
> df.show() and df.select("c").show()
>
> Mvg/Regards
> Saurabh Gulati
> Data Platform
> ------------------------------
> *From:* Sean Owen <sr...@gmail.com>
> *Sent:* 04 January 2023 14:25
> *To:* Saurabh Gulati <sa...@fedex.com>
> *Cc:* Mich Talebzadeh <mi...@gmail.com>; User <
> user@spark.apache.org>
> *Subject:* Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used
> within the data
>
> That input is just invalid as CSV for any parser. You end a quoted col
> without following with a col separator. What would the intended parsing be
> and how would it work?
>
> On Wed, Jan 4, 2023 at 4:30 AM Saurabh Gulati <sa...@fedex.com>
> wrote:
>
>
> @Sean Owen <sr...@gmail.com> Also see the example below with quotes
> feedback:
>
> "a","b","c"
> "1","",",see what ""I did"","
> "2","","abc"
>
>
Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
Posted by Saurabh Gulati <sa...@fedex.com.INVALID>.
Hi @Sean Owen<ma...@gmail.com>
Probably the data is incorrect, and the source needs to fix it.
But using python's csv parser returns the correct results.
import csv
with open("/tmp/test.csv") as c_file:
csv_reader = csv.reader(c_file, delimiter=",")
for row in csv_reader:
print(row)
['a', 'b', 'c']
['1', '', ',see what "I did",\ni am still writing']
['2', '', 'abc']
And also, I don't understand why there is a distinction in outputs from df.show() and df.select("c").show()
Mvg/Regards
Saurabh Gulati
Data Platform
________________________________
From: Sean Owen <sr...@gmail.com>
Sent: 04 January 2023 14:25
To: Saurabh Gulati <sa...@fedex.com>
Cc: Mich Talebzadeh <mi...@gmail.com>; User <us...@spark.apache.org>
Subject: Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
That input is just invalid as CSV for any parser. You end a quoted col without following with a col separator. What would the intended parsing be and how would it work?
On Wed, Jan 4, 2023 at 4:30 AM Saurabh Gulati <sa...@fedex.com>> wrote:
@Sean Owen<ma...@gmail.com> Also see the example below with quotes feedback:
"a","b","c"
"1","",",see what ""I did"","
"2","","abc"
Re: [EXTERNAL] Re: Incorrect csv parsing when delimiter used within the data
Posted by Sean Owen <sr...@gmail.com>.
That input is just invalid as CSV for any parser. You end a quoted col
without following with a col separator. What would the intended parsing be
and how would it work?
On Wed, Jan 4, 2023 at 4:30 AM Saurabh Gulati <sa...@fedex.com>
wrote:
>
> @Sean Owen <sr...@gmail.com> Also see the example below with quotes
> feedback:
>
> "a","b","c"
> "1","",",see what ""I did"","
> "2","","abc"
>
>