【发布时间】:2021-07-03 01:55:15
【问题描述】:
使用互联网信息/使用此平台的信息,我设法编写了一个小的 Python 代码,它读取 200 个 csv,然后能够区分与列索引对应的不同值。现在我有兴趣编写一个 csv/txt 文件,其中 2 列应该有一个变量 "time" 和另一个变量 "alpha.water" 。使用以下 Python 脚本,我可以编写一个变量“时间”:
# importing different modules
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
import importlib
totalt = 200 # defining the total time
for i in range (0,totalt):
df = pd.read_csv('interpolate_lwc_t_%d.csv'%i, skiprows=0)
# writting file corresponding to this data
x1 = df['Time'].values
tfile = open('test.txt', 'a')
tfile.write(str (x1))
tfile.close()
这个“时间”变量也用方括号[]写成如下:
[0.008][0.009][0.01][0.011][0.012][0.013][0.014][0.015][0.016][0.017][0.018][0.019][0.02][0.021][0.022][0.023][0.024][0.025][0.026][0.027][0.028][0.029][0.03][0.031][0.032][0.033][0.034][0.035][0.036][0.037][0.038][0.039][0.04][0.041][0.042][0.043][0.044][0.045][0.046][0.047][0.048][0.049][0.05][0.051][0.052][0.053][0.054][0.055][0.056][0.057][0.058][0.059][0.06][0.061][0.062][0.063][0.064][0.065][0.066][0.067][0.068][0.069][0.07][0.071]
有没有更清晰的方法来编写 csv/txt 文件,其中在变量“Time”和“alpha.water”的两列中写入相应的值?我期待以下输出:
Time alpha.water
0.008 0.01147
0.009 0.011472
0.010 0.011473
任何建议/评论都会有很大帮助。提前致谢。
【问题讨论】: