在给定条件下用python编写文本文件

在给定条件下用python编写文本文件,python,Python,我是python的新手,我正在尝试使用python在文本文件中编写一个给定的句子: 这是我的文件,我想在“Direct”后面的行中写“T” 如果第三列高于给定值: Cu Pd 3.87100000000000 1.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 1.00000

我是python的新手,我正在尝试使用python在文本文件中编写一个给定的句子:

这是我的文件,我想在“Direct”后面的行中写“T” 如果第三列高于给定值:

Cu Pd                                   
   3.87100000000000     
     1.0000000000000000    0.0000000000000000    0.0000000000000000
     0.0000000000000000    1.0000000000000000    0.0000000000000000
     0.0000000000000000    0.0000000000000000    1.0000000000000000
   Cu   Pd
     1     3
Selective dynamics
Direct
 -0.0042765297000522 -0.0042765297000522 -0.0042765297000522
 -0.0038509734526576  0.4960637515763522  0.4960637515763522 
  0.4960637515763522  0.4960637515763522 -0.0038509734526576
  0.4960637515763522 -0.0038509734526576  0.4960637515763522
例如:如果第三列“x”的值大于1.00,则该列后面将接收“T”,否则将接收“F”

有什么建议可以从哪里开始吗?
谢谢大家!

可能想看看这样的东西。它从一个文件读取,然后写入另一个文件。它仅在遇到“Direct”后才开始检查内容。这应该让你开始了解如何继续

def modFile(inPath, outPath, threshold):
    direct = False;
   try:
       # open input file
       with open(inPath, 'r') as ifp:
           # open output file
           with open(outPath, 'w', 'w') as ofp:
                # read each line.
                for line in ifp:
                    if not direct:
                        if line == 'Direct':
                            direct = True
                    else:
                        parts = line.split()
                        # check third value
                        if parts[2] > threshold:
                            line += ' T T T'
                    ofp.write(line + '\n')
    except IOError, e:
        print("Can't read or write from one of the files.")
        # handle in some way
        raise

这可能只是解决你全部问题的开始,但应该给你一些线索

可能想看看这样的东西。它从一个文件读取,然后写入另一个文件。它仅在遇到“Direct”后才开始检查内容。这应该让你开始了解如何继续

def modFile(inPath, outPath, threshold):
    direct = False;
   try:
       # open input file
       with open(inPath, 'r') as ifp:
           # open output file
           with open(outPath, 'w', 'w') as ofp:
                # read each line.
                for line in ifp:
                    if not direct:
                        if line == 'Direct':
                            direct = True
                    else:
                        parts = line.split()
                        # check third value
                        if parts[2] > threshold:
                            line += ' T T T'
                    ofp.write(line + '\n')
    except IOError, e:
        print("Can't read or write from one of the files.")
        # handle in some way
        raise

这可能只是解决你全部问题的开始,但应该给你一些线索

首先编写一个程序,该程序可以读取文件并适当地打印“T”或“F”。忘记写入文件,只需读取即可。你能做到吗?现在不行,但我会找。。。谢谢为了补充@AlexHall的评论,我建议大家看看Python2.7,除非你想使用Python3.5。忘记写入文件,只需读取即可。你能做到吗?现在不行,但我会找。。。谢谢为了补充@AlexHall的评论,我建议大家看看Python2.7,除非你想使用Python3.5。