使用PYTHON将一列数字作为第一列添加到已存在的包含数字的TEXT文件中



我正试图将一列4的递增倍数,例如(4 8 12 16(添加到TEXT文件的现有数字数据中。但是,当我尝试下面提到的代码时,列数据会添加到现有文本文件的数据末尾。我不能使用PANDAS,因为Abaqus软件没有它。

以下是文本文件中的现有数据示例(我在此处使用逗号分隔整数(:

53,25

55,39

7887

32,17

预期输出:

4,53,25

8,55,39

12,78,87

16、32、17

这是代码

import numpy as np
# defining the numbers i want to add to the text file
x1 = np.array(range(4,20,4)).tolist()
# opening the existing data file using **append** format
exisiting_data = open('data.txt', 'a')
# looping through the numbers to add to the text file
for eachitem in x1:
exisiting_data.write(str(eachitem)+'n')
exisiting_data.close()

输出:当我这样做的时候,我得到的输出是

53,25

55,39

7887

32,17

4

8

12

16

我认为这对你们很多人来说很简单。我现在是个初学者。感谢您的建议和回答。谢谢

您不能直接将系列作为文本文件中的第一列。你可以做的是,你可以创建第二个文件,并将你的系列(4,8,12(+数据从第一个文件写入第二个。

最新更新