Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
同一字符串不同的文件regex不起作用_Regex_Python 3.x_Pyspark - Fatal编程技术网

同一字符串不同的文件regex不起作用

同一字符串不同的文件regex不起作用,regex,python-3.x,pyspark,Regex,Python 3.x,Pyspark,我有几个文本文件都是.gz格式的。现在,我在pyspark脚本中运行以下代码: with gzip.open(filepath, 'rb') as f: file_content = f.read().decode('utf-8') Func_that_does_regex(file_content) 正则表达式不起作用(没有错误,但不会产生正确的输出),因为代码是在spark worker上执行的,所以我不知道如何调试它。但是,如果我解压缩一个文件,然后手动将内容复制粘贴到一个新的文

我有几个文本文件都是.gz格式的。现在,我在pyspark脚本中运行以下代码:

with gzip.open(filepath, 'rb') as f:
    file_content = f.read().decode('utf-8')
Func_that_does_regex(file_content)
正则表达式不起作用(没有错误,但不会产生正确的输出),因为代码是在spark worker上执行的,所以我不知道如何调试它。但是,如果我解压缩一个文件,然后手动将内容复制粘贴到一个新的文本文件(通过leafpad)中,然后再次gzip它,常规的表达式就可以工作了。如果我通过pyspark.take()方法打印字符串块,则原始文件和新文件中的两个字符串“外观”相同。我错过了什么


Python 3.5和Spark 2.0.1的发布与不同的操作系统如何处理字符串中的新行有关。以前我只在Linux中使用创建的文件,所以在正则表达式中\n就足够了。我现在得到的文件用于\r\n新行,因此我的正则表达式不再工作

\r=CR(回车)//在X之前的Mac OS中用作新行字符

\n=LF(换行)//在Unix/Mac OS X中用作新行字符


\r\n=CR+LF//在Windows中用作新行字符

如果不提供示例gz文件,则必须说明…示例gz文件:读取后不要运行“.decode('utf-8')”。确保你的正则表达式是二进制的(例如:re.compile(b'myregex'))。在spark shell中逐行测试你的正则表达式。圣诞节后将返回工作,届时将提供更新!