简体   繁体   English

假定将行写入文件的循环无效

[英]a loop that is suppose to write lines to a file isnt working

I have a very large file that looks like this: [original file][1] 我有一个很大的文件,看起来像这样:[原始文件] [1]

field number 7 (info) contains ~100 pairs of X=Y separated by ';'. 字段号7(info)包含约100对X = Y,中间用“;”分隔。

I first want to split all X=Y pairs. 我首先要拆分所有X = Y对。

Next I want to scan one pair at a time, and if X is one of 4 titles and Y is an int- I want to put them them in a dictionary. 接下来,我想一次扫描一对,如果X是4个标题之一,而Y是一个整数,我想将它们放在字典中。

After finishing going through the pairs I want to check if the dictionary contains all 4 of my titles, and if so, I want to calculate something and write it into a new file. 完成配对后,我要检查字典是否包含我所有的所有4个标题,如果是,我要计算一些内容并将其写入新文件。

This is the part of my code which suppose to do that: 这是我的代码的一部分,假设要这样做:

for row in reader:
    m = re.split(';',row[7])                                                # split the info field by ';'
    d = {}
    nl = []
    for c in m:                                                             # for each info field, split by '=', if it is one of the 4 fields wanted and the value is int- add it to a dict
        t = re.split('=',c)
        if (t[0]=='AC_MALE' or t[0]=='AC_FEMALE' or t[0]=='AN_MALE' or t[0]=='AN_FEMALE') and type(t[1])==int:
            d[t[0]] = t[1]
    if 'AC_MALE' in d and 'AC_FEMALE' in d and 'AN_MALE' in d and 'AN_FEMALE' in d:     # if the dict contain all 4 wanted fields- make a new line for the final file
        total_ac = int(d['AC_MALE'])+ int(d['AC_FEMALE'])
        total_an = int(d['AN_MALE'])+ int(d['AN_FEMALE'])
        ac_an = total_ac/total_an
        nl.extend([row[0],row[1],row[3],row[4],total_ac,total_an, ac_an])
        writer.writerow(nl)

The code is running with no errors but isnt writing anything to the file. 该代码正在运行,没有错误,但是没有向该文件写入任何内容。 Can someone figure out why? 有人可以弄清楚为什么吗?

Thanks! 谢谢!

type(t[1])==int is never true. type(t[1])==int永远都不是。 t[1] is a string, always , because you just split that object from another string. t[1] 始终是一个字符串,因为您只是将该对象与另一个字符串分开了。 It doesn't matter here if the string contains only digits and could be converted to a int . 在这里,字符串是否仅包含数字并可以转换为int都没有关系。

Test if you can convert your string to an integer, and if that fails, just move on to the next. 测试您是否可以将字符串转换为整数,如果失败,则继续进行下一个操作。 If it succeeds, add the value to your dictionary: 如果成功,则将值添加到字典中:

for c in m:
    t = re.split('=',c)
    if (t[0]=='AC_MALE' or t[0]=='AC_FEMALE' or t[0]=='AN_MALE' or t[0]=='AN_FEMALE'):
        try:
            d[t[0]] = int(t[1])
        except ValueError:
            # string could not be converted, so move on
            pass

Note that you don't need to use re.split() ; 请注意,您不需要使用re.split() use the standard str.split() method instead. 请改用标准的str.split()方法。 You don't need to test if all keys are present in your dictionary afterwards, just test if the dictionary contains 4 elements, so has a length of 4. You can also simplify the code to test the key name: 之后,您无需测试字典中是否存在所有键,只需测试字典是否包含4个元素,因此长度为4。您还可以简化代码以测试键名:

for row in reader:
    d = {}
    for key_value in row[7].split(','):
        key, value = key_value.split('=')
        if key in {'AC_MALE', 'AC_FEMALE', 'AN_MALE', 'AN_FEMALE'}:
            try:
                d[key] = int(value)
            except ValueError:
                pass
    if len(d) == 4:
        total_ac = d['AC_MALE'] + d['AC_FEMALE']
        total_an = d['AN_MALE'] + d['AN_FEMALE']
        ac_an = total_ac / total_an
        writer.writerow([
            row[0], row[1], row[3], row[4],
            total_ac, total_an, ac_an])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM