我正在尝试遍历文本文件中的每一行并执行一些操作。现在,我有一个文本文件,其中包含以下内容:
--- 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'更新: THis是我当前的代码,我只能在第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'发布于 2017-02-07 01:43:21
您的解析逻辑需要更改。下面是代码的样子,在以后的改进中可以参考它。
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;12small.txt
[{"a": "1", "c": "3", "b": "2"}, {"a": "4", "c": "6", "b": "5"}]big.txt
[{"w3": "9", "w2": "8", "w1": "7"}, {"w3": "12", "w2": "11", "w1": "10"}]发布于 2017-02-07 01:00:17
变化
elif 'modified' and 'big' in line:转到
elif 'modified' in line and 'big' in line:发布于 2017-02-07 02:06:30
您的代码中存在一些问题。
首先,你在重复你自己。大的和小的情况没有足够的差异来证明代码重复。
其次,虽然我知道你想用next(file)做什么,但你仍然需要以某种方式循环这条指令来获取下一行代码。但是等等,你已经在用for line in file做同样的事情了。
最后,在每个循环中,您将重新打开相同的文件并重新生成一个不断增加的数组。这是浪费的IO。如果您试图从file流式传输到bigwater.txt和smalljson.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)https://stackoverflow.com/questions/42073176
复制相似问题