Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文本文件中水平获取数据_Python_Text Files - Fatal编程技术网

Python 在文本文件中水平获取数据

Python 在文本文件中水平获取数据,python,text-files,Python,Text Files,我已经用Python编写了脚本,下面是代码: output=[] i=0 for i in range(0,3089879): norm = str(data_normE[i,0]) output.append(normE_string) 因此,输出如下所示: 0.01 0.023 0.456 0.9834 0.23 。。。一直到3089879排 我想写如下: 0.01 0.023 0.456 0.9834 0.23 (till 308987

我已经用Python编写了脚本,下面是代码:

output=[]
i=0
for i in range(0,3089879):
   norm = str(data_normE[i,0])
   output.append(normE_string)
因此,输出如下所示:

0.01
0.023
0.456
0.9834
0.23
。。。一直到3089879排

我想写如下:

0.01    0.023    0.456    0.9834    0.23        (till 3089879 columns).
我可以在代码中更改什么?我正在把内容写在一个文本文件中。我有很多这种类型的文件,所以尽管我知道如何使用excel,但要用十万个文件来完成这项工作是一项非常艰巨的工作。有人能帮忙吗?

有两种方法:

您可以打开一个文本文件,并使用write将for循环每次迭代的结果直接写入该文件:

with open(filename, 'w') as file:
    for i in range(3089879):
        norm = str(data_normE[i,0])
        file.write(norm + ' ')
file.write不追加换行符

或者,要在for循环之后将所有结果作为字符串使用数组,也可以使用

' '.join(output)

获取一个巨大的字符串,然后将其写入文件。我会提出第一种解决方案,因为它(内存)效率更高。

请格式化代码,使其可读。显示实际将数据写入文件的行谢谢,我可以按我想要的方式进行操作。:)