[英]How to write the output of a python script which is a set of float to a text file?
I have a python script which prints the first 105 columns of a text file. 我有一个python脚本,可打印文本文件的前105列。 All the values are float and I want to write the list into a text file.
所有值都是浮点型,我想将列表写入文本文件。 I tried the code below, but the text file is empty.
我尝试了下面的代码,但文本文件为空。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys
data = pd.read_csv('E:\Python_trials\Trial_codes\Lab_trial.txt', delimiter = '\t')
df = data.ix[:,:110]
print(df)
with open("test.txt", "w") as f:
sys.stdout.writelines(str(f))
f.flush()
f.close()
You dont write to f
. 你不写信给
f
。 You are writing to standard output. 您正在写入标准输出。 Try
f.write(...)
尝试
f.write(...)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys
data = pd.read_csv('E:\Python_trials\Trial_codes\Lab_trial.txt', delimiter = '\t')
df = data.ix[:,:110]
print(df)
with open("test.txt", "w") as f:
f.write(str(f))
# f.close() This is useless, as soon as the "with" is over, it automatically closes the file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.