如何编辑文本文件?

How to edit a text file?

我正在尝试在 python 3.7 中编辑文本文件。基本上,我有一个包含数字的文本文件 (file_1.txt) - 像这样的 3 列和 5 行

1, 10, 20
2, 20, 30
3, 30, 50
4, 35, 60
5, 50, 100

我想编辑那个文件以获得一些不同的东西,基本上是这个

1, 10, 20
2, 20, 30
3, 30, 50
4, 35, 60
5, 50, 100
6, 10, 20
7, 20, 30
8, 30, 50
9, 35, 60
10, 50, 100

复制第二列和第三列,第一列继续数字,每新行添加一个。 我试图这样做,但没有成功。这是我尝试过的:

with open("file_1.txt", "r+") as file1:
    file1.read()
    i = 6
    imax = 10
    while i <= imax:
        sentence = str(i) + "\n"
        file1.write(sentence)
        i = i + 1

我不明白如何复制第二列和第三列。

有人知道怎么做吗?

简短易懂。只有 3 行。

with open('file_1.txt', 'r+') as f:
    for num, content in enumerate(f.readlines()):
        f.write(f'{num+6}, {content[3:]}')

如果这是一个类似 csv 的文件,您可能需要使用 pandas(这是数据帧操作的最佳方式之一)。一个简单的例子:

import pandas as pd
df = pd.read_csv("<path_to_data_file>", header=None)
df = pd.concat([df, df])
df[0] = list(range(1, 11))
df.to_csv("result.csv", header=None, index=None)

Pythonic 方式:它将换行符附加到文件中。

with open('sample.txt', 'r') as f:
l = [i.strip() for i in f.readlines()]
max_row = int(l[-1].split(',')[0])

x = [str(i) for i in range(max_row+1,11)]
y = [i.split(',', 1)[-1] for i in l]

with open('sample.txt', 'a') as f:
    for item in [x[i]+',' + y[i] for i in range(len(x))]:
        f.write("%s\n" % item)

PS: 最大行可以是行数的长度

另一种方式:

with open("test.txt", "r+") as file1:
    lines = file1.readlines()
    index = 0
    i = 6
    imax = 10
    while i <= imax:
        sentence = lines[index].split(", ")[1:]
        sentence.insert(0, str(i))
        file1.write(", ".join(sentence))
        i += 1
        index += 1

输出:

1, 10, 20
2, 20, 30
3, 30, 50
4, 35, 60
5, 50, 100
6, 10, 20
7, 20, 30
8, 30, 50
9, 35, 60
10, 50, 100

首先,您需要从输入中读取所有数据,并存储它。

然后再过一遍,写入文件。

data = []

with open("file_1.txt", "r+") as file1:

    # read the data

    for line in file1:
        # .strip() to remove the newline
        # .split(", ") to split into 3 values
        # map(int, ...) to convert each from string to integer
        index, column2, column3 = map(int, line.strip().split(", "))

        #save the second and third coluumn
        data.append((column2, column3))

    # now write it back again:

    for column2, column3 in data:
        index += 1  # continue incrementing the index

        # format the lines and write them into the file
        file1.write("{}, {}, {}\n".format(index, column2, column3))

此方法直接将每一行作为字符串处理,无需拆分任何更多的列。

第一个 for 循环将第 2 列和第 3 列(带前导逗号)提取到列表中,跟踪行数。第二个循环附加此列表,从计数开始递增索引。

with open("file_1.txt", "r+") as file1:
    our_data = []
    count = 0
    for line in file1:
        first_comma_pos = line.find(',')
        # extract cols 2&3 including the leading comma
        our_data.append(line[first_comma_pos:])
        count += 1

    for i in range(count):
        sentence = str(i + count) + our_data[i] + '\n'
        file1.write(sentence)

下面的脚本将构建新文件,您可以设置要创建的行数。

首先从输入文件中读取所有行,然后将您设置的行数写入新文件。

list_emitter 可以从给定列表中无限地生成项目,因此您只需调整 output_lines_count 变量即可使输出文件更大。

def list_emitter(l):
    """This generator will endlessly yield items from given list."""
    while True:
        for item in l:
            yield item


with open('file_1.txt') as input_file:
    lines = input_file.readlines()    # Create list of lines

with open('output_file.txt', 'w') as output_file:
    output_lines_count = 10 # Set how many output lines you want
    for counter, line in enumerate(list_emitter(lines)):
        if counter == output_lines_count:
            break
        first, second, third = line.strip().split() # Parse line
        output_file.write('{}, {} {}\n'.format(counter+1, second, third))

此模块也适用:

def edit(nrows, filename):
    nrows +=1 #to avoid off-by-one error because dealing with lists

    outf = open(filename, 'a')

    column_1 = [1, 2, 3, 4, 5]
    column_2 = [10, 20, 30, 35, 50]
    column_3 = [20, 30, 50, 60, 100]

    last_column_1 = column_1[-1]
    list_1 = list(range(last_column_1+1, last_column_1+nrows))
    list_2 = nrows//len(column_2)*column_2 + column_2[0:nrows%len(column_2)]
    list_3 = nrows//len(column_3)*column_3 + column_3[0:nrows%len(column_3)]

    for c1, c2, c3 in zip(list_1, list_2, list_3):
        outf.write("{}, {}, {}\n".format(c1, c2, c3))

if __name__ == '__main__':
    edit(10, 'file.txt')

假设有一个 file.txt,文本为:

1, 10, 20
2, 20, 30
3, 30, 50
4, 35, 60
5, 50, 100