Python 在word文件中添加一个额外的列

Python 在word文件中添加一个额外的列,python,Python,我有一个如下所示的文本文件: 1 0.0 2 0.2 3 0.4 我现在要做的是检查某些值是否在阈值之间,然后在行中添加一些内容。因此,如果值为0.1,因此介于0和0.2之间,则应添加“1”,输出应为: 1 0.0 1 2 0.2 3 0.4 我试过这个: #open doc doc = "sample_name.txt" f = open(doc, "r") lines = l.readlines() count = 1 for line in lines: elements =

我有一个如下所示的文本文件:

1 0.0
2 0.2
3 0.4
我现在要做的是检查某些值是否在阈值之间,然后在行中添加一些内容。因此,如果值为0.1,因此介于0和0.2之间,则应添加“1”,输出应为:

1 0.0 1
2 0.2
3 0.4
我试过这个:

#open doc
doc = "sample_name.txt"
f = open(doc, "r")
lines = l.readlines()

count = 1 
for line in lines:

 elements = line.split(" ")
 start_time = elements[1]

 #get element from next line
 next_line = lines[count]
 elements_new_line = next_line.split(" ")
 end_time = element_new_line[1]

 if i >= end_time and i <= start_next_time:
     #add a one the file
 #increase counter
 count = count + 1
#打开文档
doc=“sample\u name.txt”
f=打开(文件“r”)
行=l.readlines()
计数=1
对于行中的行:
元素=行。拆分(“”)
开始时间=元素[1]
#从下一行获取元素
下一行=行[计数]
元素\新\行=下一行。拆分(“”)
结束时间=元素新行[1]

如果严格地说,i>=end\u time和i,那么在文本文件的行尾添加值通常并不容易。这是因为即使在一行中添加一个字符,也会将文件中的所有其他字符“推”到右侧。通常,首选的策略是读取输入文件的行,根据需要向这些行添加或修改字符,然后将它们写入输出文件。在您的情况下,可以丢弃输入文件,并将输出文件放置到位

我编写了以下代码,用于在打开输入和输出文件时使用
with
,以便在退出
with
缩进时自动关闭这些文件。现在,这是处理文件的首选方法

我的假设是每行只有两个值,分别称为
first
second
。我使用
strip
删除每行末尾的任何换行符或回车符。我使用
if
测试输入值。您将注意到,有必要将读取的字符转换为浮点值,以便与浮点值进行比较

使用
退出
时,存储区中有两个文件。我丢弃了原始版本,并将新版本重命名为原始版本的名称

with open('sample_name.txt') as sample, open('temp.txt', 'w') as temp:
    for line in sample:
        first, second = line.strip().split()
        if 0 <= float(second) < 0.2:
            temp.write('%s %s 1\n' % (first, second))
        else:
            temp.write('%s %s\n' % (first, second))

from os import remove, rename

remove('sample_name.txt')
rename('temp.txt', 'sample_name.txt')
import os
fname = "sample_name.txt"
temp_fname = fname + ".tmp"
with open(fname, 'r') as fin, open(temp_fname, 'w') as fout:
    for line in fin:
        parts = line.split()
        if 0 < float(parts[1]) < 0.2:
            parts.append("1")
            fout.write(' '.join(parts) + '\n')
        else:
            fout.write(line)
os.remove(fname)
os.rename(temp_fname, fname)
以open('sample_name.txt')作为示例,以open('temp.txt','w')作为临时:
对于样本中的行:
第一个,第二个=line.strip().split()

如果0写入修改的文件,然后覆盖原始文件

with open('sample_name.txt') as sample, open('temp.txt', 'w') as temp:
    for line in sample:
        first, second = line.strip().split()
        if 0 <= float(second) < 0.2:
            temp.write('%s %s 1\n' % (first, second))
        else:
            temp.write('%s %s\n' % (first, second))

from os import remove, rename

remove('sample_name.txt')
rename('temp.txt', 'sample_name.txt')
import os
fname = "sample_name.txt"
temp_fname = fname + ".tmp"
with open(fname, 'r') as fin, open(temp_fname, 'w') as fout:
    for line in fin:
        parts = line.split()
        if 0 < float(parts[1]) < 0.2:
            parts.append("1")
            fout.write(' '.join(parts) + '\n')
        else:
            fout.write(line)
os.remove(fname)
os.rename(temp_fname, fname)

你能再解释一点吗?举个例子我想不出一种情况,
line.strip().split()!=line.split()
,但也许你是为了教学目的而使用
strip()
。@StevenRumbalski:当然,你是对的。有点像“腰带和背带”。