如何使用Python读取文件

时间:2015-12-22 16:58:03

标签: python file-io

我正在尝试完成在文件中读取重复范围的行的任务。该行以004开头,以819结尾(这些代表订单。可能是1或1000+订单 - 重复范围)。我希望在004行获得具有特定编号的特定文件,并使所有行达到819,之后任何其他匹配的004行。我曾经尝试过,但却陷入了范围内的限制。我试图想过去,但一直在打墙。任何帮助和/或指示都表示赞赏。

我读取的文件是.txt文件,格式为:

...
004 54566
006 P56
008 Name 
010 61758012
018 UMC  
027
...
819 Staven

以下是我到目前为止阅读该行的代码:

os.chdir(r"C:\\mydirectory")
wrkdir = os.getcwd()
filelist = os.listdir(wrkdir)

sampleList = [filename for filename in filelist if filename.endswith(".SFX")]

filelist = sampleList

for sfx in filelist:
with open(sfx, 'r') as rfile:
    lines = rfile.readlines()
    for line in lines:
        count+=1
        if line[0:3] == '004':
            gstring = line.split(' ')
            #if gstring[1] == '8041': (This line did not work for me)
                print line #(ultimately I will write these lines to a new file)

我在此之后画一个空白,不确定如何在行004 - 819之间打印,如果004 =='某个数字&#39 ;?

3 个答案:

答案 0 :(得分:3)

基本上是状态机

for sfx in filelist:
    with open(rxf, 'r') as rfile:
        keep = False
        for line in rfile:
            if line.startswith('004'):
                gstring = line.split() # white space
                if '8041' in gstring:
                    keep = True

            if keep and line.startswith('819'):
                print line
                keep = False

            if keep:
                print line

答案 1 :(得分:1)

readlines也会读取新行字符,你必须在进一步处理你的行之前删除它们。

答案 2 :(得分:1)

这应该有效。在本地测试

prefix = "004"
prefix_len = len(prefix) + 1 # +1 for space
end_line_prefix = "819"
desired_value = "8041"
with open("input.txt", "r") as rfile:
    for line in rfile:
        if not line.startswith(prefix):
            continue  # if it doesnt start with 004, not interested

        val = line[prefix_len:len(line) - 1]  # at the end we also have new line
        if val != desired_value:
            continue  # if 004 doesnt have the value of our interest, skip

        while True:
            print("-> %s" % line)
            line = rfile.readline()
            if line.startswith(end_line_prefix):  # if we reached the line starting with 819, we print and then break
                print("-> %s" % line)
                break
rfile.close()