gpt4 book ai didi

python - 如何将 python 脚本的输出(一组 float )写入文本文件?

转载 作者:行者123 更新时间:2023-12-01 03:21:58 25 4
gpt4 key购买 nike

我有一个 python 脚本,可以打印文本文件的前 105 列。所有值都是 float ,我想将列表写入文本文件。我尝试了下面的代码,但文本文件是空的。

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()

最佳答案

您不写入f。您正在写入标准输出。尝试 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

关于python - 如何将 python 脚本的输出(一组 float )写入文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41826635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com