Python语言怎么把数组分行写入TXT
我想分行写入,不是一起写入[3.00008000+0.j-10.58085662-19.4778165j5.87334700+4.733817j-0.86048738-0...
我想分行写入,不是一起写入
[ 3.00008000 +0.j -10.58085662-19.4778165j 5.87334700 +4.733817j
-0.86048738 -0.5688545j 17.35029000 +0.j -0.86048738 +0.5688545j
5.87334700 -4.733817j -10.58085662+19.4778165j]
这个 我希望txt是
3.00008000 +0.j
-10.58085662-19.4778165j
5.87334700 +4.733817j
-0.86048738 -0.5688545j
17.35029000 +0.j
-0.86048738 +0.5688545j
5.87334700 -4.733817j
-10.58085662+19.4778165j 展开
[ 3.00008000 +0.j -10.58085662-19.4778165j 5.87334700 +4.733817j
-0.86048738 -0.5688545j 17.35029000 +0.j -0.86048738 +0.5688545j
5.87334700 -4.733817j -10.58085662+19.4778165j]
这个 我希望txt是
3.00008000 +0.j
-10.58085662-19.4778165j
5.87334700 +4.733817j
-0.86048738 -0.5688545j
17.35029000 +0.j
-0.86048738 +0.5688545j
5.87334700 -4.733817j
-10.58085662+19.4778165j 展开
2个回答
展开全部
def writeToTxt(list_name,file_path):
try:
fp = open(file_path,"w+")
for item in list_name:
fp.write(str(item)+"\n")//list中一项占一行
fp.close()
except IOError:
print("fail to open file")
if __name__ == "__main__":
list_name = [ 3.00008000 +0.j,-10.58085662-19.4778165j,5.87334700 +4.733817j, -0.86048738 -0.5688545j,17.35029000 +0.j,-0.86048738 +0.5688545j,5.87334700 -4.733817j,-10.58085662+19.4778165j] //你的list
file_path = r"hello.txt"
writeToTxt(list_name,file_path)
展开全部
>>> X=[ 3.00008000 +0.j , -10.58085662-19.4778165j , 5.87334700 +4.733817j,
... -0.86048738 -0.5688545j , 17.35029000 +0.j, -0.86048738 +0.5688545j,
... 5.87334700 -4.733817j , -10.58085662+19.4778165j]
>>> S='\n'.join(str(num)[1:-1] for num in X)
>>> open(r'test.txt','w').write(S)
经python2.7测试,结果与你希望的一致,测试中无错误,不玩了,休息去了
... -0.86048738 -0.5688545j , 17.35029000 +0.j, -0.86048738 +0.5688545j,
... 5.87334700 -4.733817j , -10.58085662+19.4778165j]
>>> S='\n'.join(str(num)[1:-1] for num in X)
>>> open(r'test.txt','w').write(S)
经python2.7测试,结果与你希望的一致,测试中无错误,不玩了,休息去了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询