【问题标题】:Loops in python does not loop through every line in filepython中的循环不会遍历文件中的每一行
【发布时间】:2017-02-06 16:56:18
【问题描述】:

我正在尝试遍历文本文件中的每一行并执行一些操作。现在我有一个包含以下内容的文本文件:

--- small modified --- #line 1
1,2,3                  #line 2
4,5,6                  #line 3
--- big modified ---   #line 4
7;8;9                  #line 5
10;11;12               #line 6

我试图将第 2,3 行解析到一个文件中,将第 5,6 行解析到另一个文件中,但现在,只有第 2 行和第 3 行被写入文件,并且知道为什么“elif”语句没有运行。我无法解决逻辑错误,如果有人可以帮助我,我将不胜感激。

下面是我的代码:

def convert_json(fileName):
    with open(fileName,'r') as file:
        for line in file:
            if 'modified' and 'small' in line:
                for li in file:
                    fields1 = li.split(',')
                    if len(fields1) >= 3:
                            smallarr.append({
                            "a": fields1[0],
                            "b": fields1[1],
                            "c": fields1[2]
                                })
                            with open('smalljson.txt','w+') as small_file:
                                json.dump(smallarr, small_file)
                    else:
                        pass

            elif 'modified' and 'big' in line:
                for li in file:
                    fields2 = li.split(';')
                    if len(fields2) >= 3:
                            bigarr.append({
                            "w1": fields2[0],
                            "w2": fields2[1],
                            "w3": fields2[2],
                                })
                            with open('big.txt','w+') as big_file:
                                json.dump(bigarr, big_file)
                    else: 
                        pass



            else:
                print 'test'

更新:这是我当前的代码,我能够做到,但仅适用于第 2 行和第 5 行,除了第二个 for 循环之外,我想不出另一种方法来循环这些行

def convert_json(fileName):
with open(fileName,'r') as file:
    for line in file:
        #if 'modified' in line and 'small' in line:
        if 'modified' in line and 'Small' in line:
            fields1 = next(file).split(',')
            if len(fields1) >= 3:
                smallarr.append({
                "a": fields1[0],
                "b": fields1[1],
                "c": fields1[2]
                })
                with open('smalljson.txt','w+') as small_file:
                    json.dump(smallarr, small_file)
            else:
                pass



        elif 'modified' in line and 'big' in line:
            fields2 = next(file).split(';')
            if len(fields2) >= 3:
                bigarr.append({
                "w1": fields2[0],
                "w2": fields2[1],
                "w3": fields2[2],
                })
                with open('bigwater.txt','w+') as big_file:
                    json.dump(bigarr, big_file)
            else:
                pass

        else:
            print 'test'

【问题讨论】:

  • 您尝试采取什么措施来解决此问题?您是否尝试过插入调试消息?或者使用 IDE 的调试器(如果有的话)单步执行程序?
  • 我尝试循环播放,当我在第一个 else 语句之后插入一个 break 语句时,只有第 2 行、第 5 行和第 6 行被写入。第 3 行没有写。
  • 我很确定您的内部 for 循环将遍历整个文件,耗尽您在循环之间共享的迭代器。
  • @juanpa.arrivillaga 有什么建议可以阻止内部 for 循环这样做吗?
  • 你可以break out。但是您应该使用 1 个 for 循环来执行此操作。

标签: python loops for-loop


【解决方案1】:

您的解析逻辑需要更改。这是代码的样子,供以后改进时参考。

def file_parser(self):
    file_section = 0

    smallarr = []
    bigarr = []
    with open('data.txt') as in_file:
        for in_line in in_file:
            in_line = in_line.strip()

            if 'small' in in_line:
                file_section = 1
                continue
            elif 'big' in in_line:
                file_section = 2
                continue

            if file_section == 1:
                fields1 = in_line.split(',')
                if len(fields1) >= 3:
                    smallarr.append({
                        "a": fields1[0],
                        "b": fields1[1],
                        "c": fields1[2]
                    })
            elif file_section == 2:
                fields2 = in_line.split(';')
                if len(fields2) >= 3:
                    bigarr.append({
                        "w1": fields2[0],
                        "w2": fields2[1],
                        "w3": fields2[2],
                    })

    with open('small.txt', 'w+') as small_file:
        json.dump(smallarr, small_file)

    with open('big.txt', 'w+') as big_file:
        json.dump(bigarr, big_file)

输入数据:

--- small modified ---
1,2,3
4,5,6
--- big modified ---
7;8;9
10;11;12

small.txt

[{"a": "1", "c": "3", "b": "2"}, {"a": "4", "c": "6", "b": "5"}]

大.txt

[{"w3": "9", "w2": "8", "w1": "7"}, {"w3": "12", "w2": "11", "w1": "10"}]

【讨论】:

  • 非常感谢您的帮助。它现在是一个工作程序:)
【解决方案2】:

改变

elif 'modified' and 'big' in line:

进入

elif 'modified' in line and 'big' in line:

【讨论】:

  • 我试过了,好像不行。仍然没有执行“elif”语句
  • 改变你的逻辑,并删除“for li in file:”
  • 您的第一个if 条件存在同样的缺陷。
【解决方案3】:

您的代码中存在一些问题。

首先你在重复自己。大小案例的差异不足以证明代码重复是合理的。

其次,虽然我了解您要对 next(file) 执行的操作,但您仍需要以某种方式循环该指令以获取下一行。但是等等,你已经在使用 for line in file 做到这一点。

最后,在每个循环中,您都重新打开同一个文件并减少一个不断增加的数组。这是浪费 IO。如果您尝试从 file 流式传输到 bigwater.txtsmalljson.txt 并且不在内存中存储太多内容,那么这是错误的方法,因为 json.dump 不能用于流式传输数据。

这是我的看法:

def convert_json(fileName):
    big = []
    small = []
    with open(fileName,'r') as file:
        for line in file:
            line = line.strip()
            if line.startswith("--"):
                if "big" in line:
                    array = big
                    keys = ["w1", "w2", "w3"]
                    sep = ";"
                else:
                    array = small
                    keys = ["a", "b", "c"]
                    sep = ","
                continue

            values = line.split(sep)
            # todo: make sure sizes match
            mapping = dict(zip(keys, values))
            array.append(mapping)

    with open('smalljson.txt','w') as small_file:
        json.dump(small, small_file)
    with open('bigwater.txt','w') as big_file:
        json.dump(big, big_file)

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2022-12-12
    • 2019-09-03
    相关资源
    最近更新 更多