• <bdo id='NJtAX'></bdo><ul id='NJtAX'></ul>

  • <legend id='NJtAX'><style id='NJtAX'><dir id='NJtAX'><q id='NJtAX'></q></dir></style></legend>
  • <small id='NJtAX'></small><noframes id='NJtAX'>

    <i id='NJtAX'><tr id='NJtAX'><dt id='NJtAX'><q id='NJtAX'><span id='NJtAX'><b id='NJtAX'><form id='NJtAX'><ins id='NJtAX'></ins><ul id='NJtAX'></ul><sub id='NJtAX'></sub></form><legend id='NJtAX'></legend><bdo id='NJtAX'><pre id='NJtAX'><center id='NJtAX'></center></pre></bdo></b><th id='NJtAX'></th></span></q></dt></tr></i><div id='NJtAX'><tfoot id='NJtAX'></tfoot><dl id='NJtAX'><fieldset id='NJtAX'></fieldset></dl></div>

        <tfoot id='NJtAX'></tfoot>

        只能通过 csv 阅读器迭代一次

        Can only iterate once through csv reader(只能通过 csv 阅读器迭代一次)
        <i id='z4ywP'><tr id='z4ywP'><dt id='z4ywP'><q id='z4ywP'><span id='z4ywP'><b id='z4ywP'><form id='z4ywP'><ins id='z4ywP'></ins><ul id='z4ywP'></ul><sub id='z4ywP'></sub></form><legend id='z4ywP'></legend><bdo id='z4ywP'><pre id='z4ywP'><center id='z4ywP'></center></pre></bdo></b><th id='z4ywP'></th></span></q></dt></tr></i><div id='z4ywP'><tfoot id='z4ywP'></tfoot><dl id='z4ywP'><fieldset id='z4ywP'></fieldset></dl></div>

          1. <legend id='z4ywP'><style id='z4ywP'><dir id='z4ywP'><q id='z4ywP'></q></dir></style></legend>
              <tbody id='z4ywP'></tbody>

            • <bdo id='z4ywP'></bdo><ul id='z4ywP'></ul>

              <small id='z4ywP'></small><noframes id='z4ywP'>

                <tfoot id='z4ywP'></tfoot>
                1. 本文介绍了只能通过 csv 阅读器迭代一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我基本上有一个非常长的字符串列表,以及一个包含一列字符串和一列数字的 CSV 文件.我需要遍历非常长的字符串列表,并且对于每个字符串,循环遍历 CSV 文件的行,检查 CSV 第一列中的每个字符串,看看它是否出现在我的字符串中,如果出现,添加另一列中的数字到某事.一个最小的例子是

                  So I basically have an extremely long list of strings, and a CSV file that contains a column of strings and a column of numbers. I need to loop through the extremely long list of strings, and for each one, loop through the rows of the CSV file checking each string in the first column of the CSV to see if it occurs in my string, and if it does, add the number in the other column to something. A minimal sort of example would be

                  import csv
                  sList = ['a cat', 'great wall', 'mediocre wall']
                  vals = []
                  with open('file.csv', 'r') as f:
                      r = csv.reader(f)
                      for w in sList:
                          val = 0
                          for row in r:
                              if row[0] in w:
                                  val += 1
                          vals.append(val)
                  

                  我可能会使用它的 CSV 文件示例

                  An example of a CSV file with which I might use this could be

                  a, 1
                  great, 2
                  

                  当然 csv.reader(f) 创建了一个我只能循环一次的可迭代对象.我在其他地方看到了使用 itertools 的建议,但我发现的所有建议都是针对涉及少量循环 CSV 文件的问题,通常只有两次.如果我多次尝试使用它来循环遍历 CSV,我不确定这对内存消耗意味着什么,总的来说,我只是想知道解决这个问题的最聪明的方法.

                  Of course csv.reader(f) creates an iterable that I can loop through only once. I've seen recommendations elsewhere to use itertools but all of the recommendations I've found have been for problems that involve looping through the CSV file a small number of times, usually just twice. If I tried to use this to loop through the CSV many times I'm unsure of what that would mean for memory consumption, and in general I'm just wondering about the smartest way to approach this problem.

                  推荐答案

                  你需要重置"文件迭代器:

                  You need to "reset" the file iterator:

                  import csv
                  sList = ['a cat', 'great wall', 'mediocre wall']
                  vals = []
                  with open('data.csv', 'r') as f:
                      r = csv.reader(f)
                      for w in sList:
                          val = 0
                          f.seek(0)  #<-- set the iterator to beginning of the input file
                          for row in r:
                              print(row)
                              if row[0] in w:
                                  val += 1
                          vals.append(val)
                  

                  这篇关于只能通过 csv 阅读器迭代一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                      <legend id='HCiMl'><style id='HCiMl'><dir id='HCiMl'><q id='HCiMl'></q></dir></style></legend>
                      <i id='HCiMl'><tr id='HCiMl'><dt id='HCiMl'><q id='HCiMl'><span id='HCiMl'><b id='HCiMl'><form id='HCiMl'><ins id='HCiMl'></ins><ul id='HCiMl'></ul><sub id='HCiMl'></sub></form><legend id='HCiMl'></legend><bdo id='HCiMl'><pre id='HCiMl'><center id='HCiMl'></center></pre></bdo></b><th id='HCiMl'></th></span></q></dt></tr></i><div id='HCiMl'><tfoot id='HCiMl'></tfoot><dl id='HCiMl'><fieldset id='HCiMl'></fieldset></dl></div>
                          <bdo id='HCiMl'></bdo><ul id='HCiMl'></ul>

                          <tfoot id='HCiMl'></tfoot>
                            <tbody id='HCiMl'></tbody>

                          • <small id='HCiMl'></small><noframes id='HCiMl'>