使用循环导入文本文件

时间:2019-07-19 07:54:53

标签: python-2.7

我想使用循环而不是一个一个地导入文本文件。

我有9个标记为clump0.txt-clump9.txt的文本文件,导入后我必须分配一个变量。

ymport.textClumps('/tmp/clump1.txt')
#c0 = getClumpInfo()
c0 = pack.SpherePack()
c0.fromSimulation()
O.resetThisScene()

ymport.textClumps('/tmp/clump1.txt')
#c1 = getClumpInfo()
c1 = pack.SpherePack()
c1.fromSimulation()
O.resetThisScene()

ymport.textClumps('/tmp/clump2.txt')
#c1 = getClumpInfo()
c2 = pack.SpherePack()
c2.fromSimulation()
O.resetThisScene()

ymport.textClumps('/tmp/clump3.txt')
#c1 = getClumpInfo()
c3 = pack.SpherePack()
c3.fromSimulation()
O.resetThisScene()
...

稍后我在此使用c0-c9:

test = sp.makeClumpCloud(minCorner, maxCorner, [c0,c1,c2,c3,c4,c5,c6,c7,c8,c9], periodic=True, num = -1)

我已经尝试过:

# load clumps
arr = [c1, c2, c3, c4, c5, c6, c7, c8, c9]
for i in range (0, 9):
    ymport.textClumps("/tmp/clump{}.txt".format(i))
    arr[i] =pack.SpherePack()
    arr[i].fromSimulation()
    O.resetThisScene() 

1 个答案:

答案 0 :(得分:0)

好的,我解决了

负载团块

arr = [0,0,0,0,0,0,0,0,0]

for i in range (0, 9):
    ymport.textClumps(str(os.getcwd()) + "/tmp/clump{}.txt".format(i))
    arr[i] = pack.SpherePack()
    arr[i].fromSimulation()
    O.resetThisScene()

我只需要在数组中分配一个占位符:)谢谢