我正在尝试在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
我不明白如何复制第二列和第三列。
有人知道该怎么做吗?
答案 0 :(得分:2)
如果这是一个类似于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)
答案 1 :(得分:1)
Python方式:它将换行符添加到文件中。
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:最大行可以是行数的长度
答案 2 :(得分:0)
简短易懂。只需三行。
with open('file_1.txt', 'r+') as f:
for num, content in enumerate(f.readlines()):
f.write(f'{num+6}, {content[3:]}')
答案 3 :(得分:0)
另一种方式:
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
答案 4 :(得分:0)
首先,您需要从输入中读取所有数据,并将其存储。
然后再次遍历并将其写入文件。
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))
答案 5 :(得分:0)
这种方法可以直接将每一行作为字符串使用,而不必拆分多余的列。
第一个for循环将Cols 2&3(以逗号开头)扩展为List,跟踪行数。第二个循环从计数开始附加此列表递增索引。
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)
答案 6 :(得分:0)
下面的脚本将构建新文件,您可以设置要创建的行数。
首先从输入文件中读取所有行,然后将您设置的行数写入新文件。
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))
答案 7 :(得分:0)
此模块也可以工作:
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