0

I am using Jupyter and which is working on pyspark(python).

I have used "for" loop to iterate the process and trying to save the file after each iteration.

for example:

name = "mea"
for i in range(2):
    print "name[i]"
    i +=1

and output is:

name[i]
name[i]

this above algorithm is the short explaination related to the main algorithm that i am working on.

the problem is it is giving an output name[i] and I want it to give me name1 and for second iteration name[2].

I need to use " " because i wanted to save my file to specific folder and i need to speacify the path in " ". So after firsdt iteration it should save the file as name1 and after second iteration it should save the file as name[2]. enter image description here

so from image in my actual algorithm, result is the output that i am getiing after each for loop iteration and for each output, i wanted to save it in new files like result[0],result1,result[2] instead of result[i],result[i],result[i]. because the latter one, it is replacing the file to the old one.

Community
  • 1
  • 1
Luke
  • 21
  • 4

2 Answers2

0

I guess it has nothing specific to pyspark that you are trying to achieve. As per your example, what you need is - use of variable in strings, so this will suffice for your example:

name = "mea"
for i in range(2):
    print "name[%s]" % i  
    i +=1
joshi.n
  • 489
  • 3
  • 7
-1

You can modify your print statement as follows

print "name[" + str(i) + "]"
sat
  • 603
  • 3
  • 6