Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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
Python AttributeError:'str'对象没有属性'name'PySpark_Python_Python 3.x_Pyspark - Fatal编程技术网

Python AttributeError:'str'对象没有属性'name'PySpark

Python AttributeError:'str'对象没有属性'name'PySpark,python,python-3.x,pyspark,Python,Python 3.x,Pyspark,我已创建列表并尝试将其分配给StructType,但出现错误: AttributeError: 'str' object has no attribute 'name' 我的代码: from pyspark.sql import SparkSession import logging from pyspark.sql.types import * from pyspark.sql.functions import to_timestamp from pyspark.sql.functions

我已创建列表并尝试将其分配给StructType,但出现错误:

AttributeError: 'str' object has no attribute 'name'
我的代码:

from pyspark.sql import SparkSession
import logging
from pyspark.sql.types import *
from pyspark.sql.functions import to_timestamp
from pyspark.sql.functions import udf
from pyspark.sql.functions import lit
from pyspark.sql.functions import year, month, dayofmonth
from pyspark.context import SparkContext
from pyspark.sql import SQLContext
import argparse

logging.basicConfig(level=logging.INFO,filename = 'parquet.log')
logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser()
parser.add_argument('--schema_py', '--list', nargs='+', required=True, dest='schema_py', help='Scheam def')

args = parser.parse_args()

schemaField = args.schema_py
print(type(schemaField))   #It will print <class 'list'>

schema = StructType(schemaField) # On this line facing issue
print(type(schema))
输出

$ python tst.py --schema_py 'StructField('col1', StringType(), True),StructField('col2', StringType(), True),StructField('col3', StringType(), True),StructField('col4', StringType(), True),'

<class 'list'>
Traceback (most recent call last):
  File "brrConvertParquet.py", line 41, in <module>
    schema = StructType(schemaField)
  File "/home/sysbrrd/anaconda3/lib/python3.6/site-packages/pyspark/sql/types.py", line 484, in __init__
    self.names = [f.name for f in fields]
  File "/home/sysbrrd/anaconda3/lib/python3.6/site-packages/pyspark/sql/types.py", line 484, in <listcomp>
    self.names = [f.name for f in fields]
AttributeError: 'str' object has no attribute 'name'

请帮助我了解这里出了什么问题。

我看到的问题是:

您正在向StructType调用传递str,而不是[StructField]的列表,或者因为您有nargs='+',可能您正在传递字符串列表。即 [StructField'col1',StringType,True,StructField'col2', StringType,True,StructField'col3',StringType, True,StructField'col4',StringType,True]。 如果您确实希望以cmd arg的形式接收字段,那么您应该检查验证该arg并将其转换为所需的python类型。您可以查看json、pickle、eval或exec。 除此之外,其他一切都应该起作用

self.names=[f.name for f in fields]中断,因为fields是str而不是StructField的列表,如果它是StructField的列表,那么f.name调用应该可以正常工作:-


我希望这能有所帮助。

我看到的问题是:

您正在向StructType调用传递str,而不是[StructField]的列表,或者因为您有nargs='+',可能您正在传递字符串列表。即 [StructField'col1',StringType,True,StructField'col2', StringType,True,StructField'col3',StringType, True,StructField'col4',StringType,True]。 如果您确实希望以cmd arg的形式接收字段,那么您应该检查验证该arg并将其转换为所需的python类型。您可以查看json、pickle、eval或exec。 除此之外,其他一切都应该起作用

self.names=[f.name for f in fields]中断,因为fields是str而不是StructField的列表,如果它是StructField的列表,那么f.name调用应该可以正常工作:-


我希望这有帮助。

:-谢谢你的回答!我能把这个问题分类,问题和你在这里解释的一样。由于我传递的数据类型是字符串格式的,因此我已将其类型转换为所需的Pypark数据类型,并且有效。@SwapnilM如果答案正确,并且描述了您遇到的问题的解决方案,也许您应该将其标记为已回答。至少堆栈溢出是这样工作的。:-谢谢你的回答!我能把这个问题分类,问题和你在这里解释的一样。由于我传递的数据类型是字符串格式的,因此我已将其类型转换为所需的Pypark数据类型,并且有效。@SwapnilM如果答案正确,并且描述了您遇到的问题的解决方案,也许您应该将其标记为已回答。至少堆栈溢出是这样工作的。