Ⅰ python将输出数组转化为一个新的文本文档
fout=open('output.txt','w')#打开输出文件
...
print>>fout,what_you_want_to_print#所有的print语句都如此修改
...
fout.close()#最后关闭文件
这是python2.7中基本的写文件操作。
Ⅱ python将数组写入excel文件
# 将数据写入新文件
def data_write(file_path, datas):
f = xlwt.Workbook()
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet
#将数据写入第 i 行,第 j 列
i = 0
for data in datas:
for j in range(len(data)):
sheet1.write(i,j,data[j])
i = i + 1
f.save(file_path) #保存文件
Ⅲ Python语言怎么把数组分行写入TXT
defwriteToTxt(list_name,file_path):
try:
fp=open(file_path,"w+")
foriteminlist_name:
fp.write(str(item)+" ")//list中一项占一行
fp.close()
exceptIOError:
print("failtoopenfile")
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)
Ⅳ python 数组写入文件格式控制
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)
Ⅳ 用Python写txt文件,若该行数据存在则不写入;若不存在,则写入。怎么才能在写入之前判断该数据是否存在
你好:
你可以先读取txt文件,然后将数据放到一个数组中;
执行写入的时候再做判断;
Ⅵ python中读取一个.txt文件,并且放入数组。
#python3.3
file=open('test.txt')
lines=file.readlines()
aa=[]
forlineinlines:
temp=line.replace('"','').split(',')
del(temp[0])
del(temp[2])
aa.append(temp)
print(aa)#2.x请将此行改为printaa
Ⅶ 怎么用python完成以下内容
答: 首先本题分为两个内容,第1个创建10X10的随机数组,并将其存储到 Txt文件中,其次再将其读出进行相关统计分析。具体代码如下所示。
Ⅷ python怎么把数组数据写入表格文档举个例子就行
阅读数:1564
[python]view plain
arrs=[2,15,48,4,5,6,7,6,4,1,2,3,6,6,7,4,6,8]
f=open('test.txt','w+')
count=0
fortempinarrs:
count+=1
print>>f,temp,
if(count%3==0):
print>>f
f.close()
结果:
Ⅸ python如何将字典写入文件(字典中存放的是一些二维数组),再从上述文件读数据到字典
importpickle
dictfile=open("myfile",'wb')
mydict={"a":[[1,2],[3,4]],"b":[[5,6],[7,8]]}
pickle.mp(mydict,dictfile)
dictfile.close()
dictfile=open("myfile",'rb')
readdict=pickle.load(dictfile)
print(readdict)