相关疑难解决方法(0)

在python中,如果函数没有return语句,它返回什么?

鉴于此示例函数:

def writeFile(listLine,fileName):
    '''put a list of str-line into a file named fileName'''
    with open(fileName,'a',encoding = 'utf-8') as f:
        for line in listLine:
            f.writelines(line+'\r\n')
    return True
Run Code Online (Sandbox Code Playgroud)

这个return True陈述有用吗?

它和没有它有什么区别?如果没有返回功能会发生什么?

python python-3.x

16
推荐指数
1
解决办法
2万
查看次数

从PySpark向Redis写入数据

在Scala中,我们会像这样写一个RDD给Redis:

datardd.foreachPartition(iter => {
      val r = new RedisClient("hosturl", 6379)
      iter.foreach(i => {
        val (str, it) = i
        val map = it.toMap
        r.hmset(str, map)
      })
    })
Run Code Online (Sandbox Code Playgroud)

我尝试在PySpark中这样做:datardd.foreachPartition(storeToRedis),其中function storeToRedis定义为:

def storeToRedis(x):
    r = redis.StrictRedis(host = 'hosturl', port = 6379)
    for i in x:
        r.set(i[0], dict(i[1]))
Run Code Online (Sandbox Code Playgroud)

它给了我这个:

ImportError:('没有名为redis的模块',函数subimport在0x47879b0,('redis',))

当然,我已经进口了redis.

python apache-spark pyspark

4
推荐指数
1
解决办法
4001
查看次数

标签 统计

python ×2

apache-spark ×1

pyspark ×1

python-3.x ×1