readline skips first line in for loop

前端 未结 1 1834
渐次进展
渐次进展 2021-01-29 12:03

I\'ve got a problem with a for loop that skips the first line. I know why but I don\'t know how to fix it. And when I change it to a while loop, or in

相关标签:
1条回答
  • 2021-01-29 12:44

    Looping over the file already reads lines. You don't need nor should you call f.readline() again. You already have the line in line, so just use that.

    Moreover, you appear to have tab-separated data; consider using the csv module to split your rows; it can also convert your data to floats for you:

    reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONNUMERIC)
    for row, (b, c) in enumerate(reader, 1):
        worksheet.write(row, 0, b)
        worksheet.write(row, 1, c)
    

    The above uses enumerate() to give you a counter per data row as well. The csv.QUOTE_NONNUMERIC quoting option causes anything not in quotes in your input file to be auto-converted to float objects.

    0 讨论(0)
提交回复
热议问题