簡體   English   中英

如何在此文件中的每 4 個元素之后將 append 和 output 轉換為 txt 文件

[英]How to append an output to a txt file after every 4th element in this file

我很抱歉,但我是 Python 的新手,我有文件包含這樣的數據

x1 y1 z1 w1 x2 y2 z2 w2 .. xn yn zn wn

我想在每個 w 之后 append 一些數字。 所以基本上在txt文件中的每4個元素之后。

請問有什么建議嗎? 非常感謝

更新:txt文件中的數據都是字符串。 我能夠轉換它們

f = open("test.txt","r+").readlines()
for line in f:
    tmp = line.strip().split(",")
    values = [float(v) for v in tmp]
    my_data = [1 1 2 23 1]
    a = np.insert(a,slice(0,None,4),my_data)  
    np.savetxt(filename, a)

附加部分仍然不起作用。

您必須首先將此文件讀入數組,插入項目並將其保存回來(假設您的文本文件的名稱是filename ):

import numpy as np
your_number = #number you want to insert OR a list of numbers you want to insert consecutively in those locations
a = numpy.loadtxt(filename)
a = np.insert(a,slice(0,None,4),your_number)
np.savetxt(filename, a)

例子:

a = np.zeros(10)
#[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
l = [1,2,3]
a = np.insert(a,slice(0,None,4),l)

output

[1. 0. 0. 0. 0. 2. 0. 0. 0. 0. 3. 0. 0.]

如果您不完全確定 w 元素是否像您說的那樣存在,那么快速而骯臟。 “數據”是您正在讀取的文件。我假設您的數據是在每一行都像您所說的格式那樣被讀取的。 我們在默認空白處分割線並獲取線的數組表示。 然后我們遍歷每個數組,並在找到匹配項的地方用新詞替換舊詞。

注意:字符串是不可變的,因此最好使用 enumerate 之類的方式將數組中的單詞實際替換為新單詞。

  with open("data", "r") as f:
       tot_lines = [line.split() for line in f]
       for line in tot_lines:
           for key, word in enumerate(line):
               if word[0] == "w":
                   line[key] = word + str(9999)
       print(tot_lines)

你的問題就是你的問題。 首先,你說:

我有文件有這樣的數據

x1 y1 z1 w1 x2 y2 z2 w2 .. xn yn zn wn

但是在你的代碼中你這樣做: split(",")所以你的數據看起來像:

x1,y1,z1,w1,x2,y2,z2,w2,...,xn,yn,zn,wn

你希望你的數據看起來像:

x1,y1,z1,w1,v1,x2,y2,z2,w2,v2,...,xn,yn,zn,wn,vn

vn值的來源:

my_data = [1 1 2 23 1]

我們注意到這不是有效的 Python 語法,因此您發布的代碼實際上並沒有運行。 對於多行輸入,少量數據似乎也很奇怪,但讓我們用它 go 吧。 我們正在查看五組四個數據項或每行 20 個數字作為輸入。 例如,如果我們有一個五行文件,我們會看到如下內容:

> cat test.txt
47,18,96,31,48,33,64,21,92,35,78,62,56,23,25,47,35,9,15,9
34,38,64,72,66,69,18,57,92,3,58,17,96,19,53,63,97,86,24,41
2,52,22,59,27,58,82,45,90,24,26,51,47,43,17,14,8,54,4,58
13,99,78,61,99,8,65,10,62,56,91,66,45,18,41,50,75,95,62,80
48,30,18,46,93,82,25,15,93,1,45,88,22,97,54,47,54,64,16,91
>

附加部分仍然不起作用。

沒關系,因為在此處附加 go 確實不是正確的方法。 要插入我們的新數據,使用基本的 Python sans numpy,我會這樣做:

my_data = [1, 1, 2, 23, 1]

with open("test.txt") as input_file:
    with open("revised.txt", 'w') as output_file:

        for line in input_file:
            array = line.rstrip().split(',')

            for index, datum in enumerate(my_data, 1):
                array.insert(index * 5 - 1, str(datum))

            print(','.join(array), file=output_file)

(索引數學index * 5 - 1很棘手,因為隨着我們添加每個新項目,數組索引會發生變化。)使用生成的 output:

> cat revised.txt 
47,18,96,31,1,48,33,64,21,1,92,35,78,62,2,56,23,25,47,23,35,9,15,9,1
34,38,64,72,1,66,69,18,57,1,92,3,58,17,2,96,19,53,63,23,97,86,24,41,1
2,52,22,59,1,27,58,82,45,1,90,24,26,51,2,47,43,17,14,23,8,54,4,58,1
13,99,78,61,1,99,8,65,10,1,62,56,91,66,2,45,18,41,50,23,75,95,62,80,1
48,30,18,46,1,93,82,25,15,1,93,1,45,88,2,22,97,54,47,23,54,64,16,91,1
>

如果這不是您想要做的,請重寫您的問題,澄清您的文件格式,明確說明您的目標並提供好的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM