我试图在我的txt文件中添加值为'0'的新行,使行数可被3整除,代码如下:
import pandas as pd
# read text file into pandas DataFrame
def process():
df = pd.read_csv("./calibration.txt", sep=' ', header=None)
if len(df)%3!=0:
print("add zero in the last")
number = 0
with open('./calibration.txt', 'a') as f:
f.write('\n%d' % number )
f.close()
#else:
# print("dont add zero")
if __name__=='__main__':
process()
df = pd.read_csv("./calibration.txt", sep=" ", header=None)
df_new = pd.DataFrame(df.iloc[: , 0].values.reshape((-1, 3)))
pd.set_option("display.max_rows", None, "display.max_columns", None)
df_new.to_csv( index=False)
print(df_new)
现在的问题是它只写一个新的行与值'0',但我需要继续写新的行与值'0',直到总数的行数得到整除3.任何帮助将是非常值得赞赏的,因为我是新的发挥行和列. TIA
1条答案
按热度按时间qhhrdooz1#
尝试以下内容