Print the non-Ascii column value, python- spark












-1














Pretty new to python and spark, I have written a udf to remove the non-ascii character if it is present in the string.



What is the most effective way to make it print the erroraneous value along with doing the operation? ( Error values would be cells that contain non-ascii characters)



code:



import findspark
findspark.init()
import pyspark # only run after findspark.init()
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
import pandas as pd
sc = spark.sparkContext

from pyspark.sql.window import Window
from pyspark.sql.functions import count, col
from pyspark.sql import Row
from pyspark.sql.functions import udf
def nonasciitoascii(unicodestring):
return unicodestring.encode("ascii","ignore")

df=spark.read.csv("abc.csv")
df.show()

df.printSchema()

convertedudf = udf(nonasciitoascii)
converted = df.select('_c1','_c2').withColumn('converted',convertedudf(df._c1))
converted.show()









share|improve this question
























  • What is the erroneous value here? Write an example of it, please.
    – OmG
    Nov 10 at 18:35










  • the error values are the cells that contain non-ascii @OmG
    – Viv
    Nov 11 at 5:43
















-1














Pretty new to python and spark, I have written a udf to remove the non-ascii character if it is present in the string.



What is the most effective way to make it print the erroraneous value along with doing the operation? ( Error values would be cells that contain non-ascii characters)



code:



import findspark
findspark.init()
import pyspark # only run after findspark.init()
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
import pandas as pd
sc = spark.sparkContext

from pyspark.sql.window import Window
from pyspark.sql.functions import count, col
from pyspark.sql import Row
from pyspark.sql.functions import udf
def nonasciitoascii(unicodestring):
return unicodestring.encode("ascii","ignore")

df=spark.read.csv("abc.csv")
df.show()

df.printSchema()

convertedudf = udf(nonasciitoascii)
converted = df.select('_c1','_c2').withColumn('converted',convertedudf(df._c1))
converted.show()









share|improve this question
























  • What is the erroneous value here? Write an example of it, please.
    – OmG
    Nov 10 at 18:35










  • the error values are the cells that contain non-ascii @OmG
    – Viv
    Nov 11 at 5:43














-1












-1








-1







Pretty new to python and spark, I have written a udf to remove the non-ascii character if it is present in the string.



What is the most effective way to make it print the erroraneous value along with doing the operation? ( Error values would be cells that contain non-ascii characters)



code:



import findspark
findspark.init()
import pyspark # only run after findspark.init()
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
import pandas as pd
sc = spark.sparkContext

from pyspark.sql.window import Window
from pyspark.sql.functions import count, col
from pyspark.sql import Row
from pyspark.sql.functions import udf
def nonasciitoascii(unicodestring):
return unicodestring.encode("ascii","ignore")

df=spark.read.csv("abc.csv")
df.show()

df.printSchema()

convertedudf = udf(nonasciitoascii)
converted = df.select('_c1','_c2').withColumn('converted',convertedudf(df._c1))
converted.show()









share|improve this question















Pretty new to python and spark, I have written a udf to remove the non-ascii character if it is present in the string.



What is the most effective way to make it print the erroraneous value along with doing the operation? ( Error values would be cells that contain non-ascii characters)



code:



import findspark
findspark.init()
import pyspark # only run after findspark.init()
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
import pandas as pd
sc = spark.sparkContext

from pyspark.sql.window import Window
from pyspark.sql.functions import count, col
from pyspark.sql import Row
from pyspark.sql.functions import udf
def nonasciitoascii(unicodestring):
return unicodestring.encode("ascii","ignore")

df=spark.read.csv("abc.csv")
df.show()

df.printSchema()

convertedudf = udf(nonasciitoascii)
converted = df.select('_c1','_c2').withColumn('converted',convertedudf(df._c1))
converted.show()






python pyspark ascii






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 5:44

























asked Nov 10 at 14:22









Viv

3951317




3951317












  • What is the erroneous value here? Write an example of it, please.
    – OmG
    Nov 10 at 18:35










  • the error values are the cells that contain non-ascii @OmG
    – Viv
    Nov 11 at 5:43


















  • What is the erroneous value here? Write an example of it, please.
    – OmG
    Nov 10 at 18:35










  • the error values are the cells that contain non-ascii @OmG
    – Viv
    Nov 11 at 5:43
















What is the erroneous value here? Write an example of it, please.
– OmG
Nov 10 at 18:35




What is the erroneous value here? Write an example of it, please.
– OmG
Nov 10 at 18:35












the error values are the cells that contain non-ascii @OmG
– Viv
Nov 11 at 5:43




the error values are the cells that contain non-ascii @OmG
– Viv
Nov 11 at 5:43












1 Answer
1






active

oldest

votes


















-1














A simple solution which works for most of the time is running the computation for the purpose:



# in python 3
def check_ascii(string):
if(not string.isascii()):
return string
else:
return None

def check_ascii_in_python_2(string):
if(not all(ord(char) < 128 for char in string)):
return string
else:
return None

all_strings_with_non_ascii_chars = df.select('_c1','_c2').withColumn('check', check_ascii(df._c1)).filter('check is not null').select('check')
all_strings_with_non_ascii_chars.show()





share|improve this answer























  • Thanks @OmG, but i get File "/opt/apps/spark-2.3.1-bin-hadoop2.7/bin/csvparser.py", line 28, in check_ascii if(not string.isascii()): AttributeError: 'unicode' object has no attribute 'isascii' at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:298) at org.apache.spark.sql.execution.python.PythonUDFRunner$$anon$1.read(PythonUDFRunner.scala:83)
    – Viv
    Nov 12 at 7:23










  • @Viv it's in python 3. For python_2 is updated.
    – OmG
    Nov 12 at 12:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239883%2fprint-the-non-ascii-column-value-python-spark%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














A simple solution which works for most of the time is running the computation for the purpose:



# in python 3
def check_ascii(string):
if(not string.isascii()):
return string
else:
return None

def check_ascii_in_python_2(string):
if(not all(ord(char) < 128 for char in string)):
return string
else:
return None

all_strings_with_non_ascii_chars = df.select('_c1','_c2').withColumn('check', check_ascii(df._c1)).filter('check is not null').select('check')
all_strings_with_non_ascii_chars.show()





share|improve this answer























  • Thanks @OmG, but i get File "/opt/apps/spark-2.3.1-bin-hadoop2.7/bin/csvparser.py", line 28, in check_ascii if(not string.isascii()): AttributeError: 'unicode' object has no attribute 'isascii' at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:298) at org.apache.spark.sql.execution.python.PythonUDFRunner$$anon$1.read(PythonUDFRunner.scala:83)
    – Viv
    Nov 12 at 7:23










  • @Viv it's in python 3. For python_2 is updated.
    – OmG
    Nov 12 at 12:40
















-1














A simple solution which works for most of the time is running the computation for the purpose:



# in python 3
def check_ascii(string):
if(not string.isascii()):
return string
else:
return None

def check_ascii_in_python_2(string):
if(not all(ord(char) < 128 for char in string)):
return string
else:
return None

all_strings_with_non_ascii_chars = df.select('_c1','_c2').withColumn('check', check_ascii(df._c1)).filter('check is not null').select('check')
all_strings_with_non_ascii_chars.show()





share|improve this answer























  • Thanks @OmG, but i get File "/opt/apps/spark-2.3.1-bin-hadoop2.7/bin/csvparser.py", line 28, in check_ascii if(not string.isascii()): AttributeError: 'unicode' object has no attribute 'isascii' at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:298) at org.apache.spark.sql.execution.python.PythonUDFRunner$$anon$1.read(PythonUDFRunner.scala:83)
    – Viv
    Nov 12 at 7:23










  • @Viv it's in python 3. For python_2 is updated.
    – OmG
    Nov 12 at 12:40














-1












-1








-1






A simple solution which works for most of the time is running the computation for the purpose:



# in python 3
def check_ascii(string):
if(not string.isascii()):
return string
else:
return None

def check_ascii_in_python_2(string):
if(not all(ord(char) < 128 for char in string)):
return string
else:
return None

all_strings_with_non_ascii_chars = df.select('_c1','_c2').withColumn('check', check_ascii(df._c1)).filter('check is not null').select('check')
all_strings_with_non_ascii_chars.show()





share|improve this answer














A simple solution which works for most of the time is running the computation for the purpose:



# in python 3
def check_ascii(string):
if(not string.isascii()):
return string
else:
return None

def check_ascii_in_python_2(string):
if(not all(ord(char) < 128 for char in string)):
return string
else:
return None

all_strings_with_non_ascii_chars = df.select('_c1','_c2').withColumn('check', check_ascii(df._c1)).filter('check is not null').select('check')
all_strings_with_non_ascii_chars.show()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 12:39

























answered Nov 11 at 10:29









OmG

7,76352643




7,76352643












  • Thanks @OmG, but i get File "/opt/apps/spark-2.3.1-bin-hadoop2.7/bin/csvparser.py", line 28, in check_ascii if(not string.isascii()): AttributeError: 'unicode' object has no attribute 'isascii' at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:298) at org.apache.spark.sql.execution.python.PythonUDFRunner$$anon$1.read(PythonUDFRunner.scala:83)
    – Viv
    Nov 12 at 7:23










  • @Viv it's in python 3. For python_2 is updated.
    – OmG
    Nov 12 at 12:40


















  • Thanks @OmG, but i get File "/opt/apps/spark-2.3.1-bin-hadoop2.7/bin/csvparser.py", line 28, in check_ascii if(not string.isascii()): AttributeError: 'unicode' object has no attribute 'isascii' at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:298) at org.apache.spark.sql.execution.python.PythonUDFRunner$$anon$1.read(PythonUDFRunner.scala:83)
    – Viv
    Nov 12 at 7:23










  • @Viv it's in python 3. For python_2 is updated.
    – OmG
    Nov 12 at 12:40
















Thanks @OmG, but i get File "/opt/apps/spark-2.3.1-bin-hadoop2.7/bin/csvparser.py", line 28, in check_ascii if(not string.isascii()): AttributeError: 'unicode' object has no attribute 'isascii' at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:298) at org.apache.spark.sql.execution.python.PythonUDFRunner$$anon$1.read(PythonUDFRunner.scala:83)
– Viv
Nov 12 at 7:23




Thanks @OmG, but i get File "/opt/apps/spark-2.3.1-bin-hadoop2.7/bin/csvparser.py", line 28, in check_ascii if(not string.isascii()): AttributeError: 'unicode' object has no attribute 'isascii' at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:298) at org.apache.spark.sql.execution.python.PythonUDFRunner$$anon$1.read(PythonUDFRunner.scala:83)
– Viv
Nov 12 at 7:23












@Viv it's in python 3. For python_2 is updated.
– OmG
Nov 12 at 12:40




@Viv it's in python 3. For python_2 is updated.
– OmG
Nov 12 at 12:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239883%2fprint-the-non-ascii-column-value-python-spark%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check