擅长:python、mysql、java
<p>如果我正确地理解了这个问题,那么您就是在问您应该将您的<code>strip_list</code>函数应用到哪里,以便将要导入的CSV数据都变成剥离字符串。关键的地方是你有“数据”列表,你从CSV阅读器。这将始终是一个字符串列表,因此您可以在将其附加到结果数组之前将其传递给<code>strip_list</code>函数。在</p>
<p>顺便说一下,您目前已经在<code>if</code>语句的两个分支中复制了CSV读取循环的核心部分。那是不必要的。以下是如何在不复制的情况下完成此操作:</p>
<pre><code>if filetype:
next(reader)
print("CSV file located, headers present, importing data")
else:
print("CSV file located, no headers found, importing data")
time.sleep(3)
for data in reader:
csvreaderlist.append(strip_list(data)) # do stripping here!
csvfilecounter = csvfilecounter +1
summarycounter = summarycounter +1
</code></pre>
<p>请注意,如果要在模块级(而不是作为函数的一部分)运行<code>strip_list</code>的定义,则需要将其移到该代码上方的某个位置。在</p>
<p>顺便说一下,您当前正在传递<code>True</code>或<code>False</code>(返回值<code>csv.Sniffer().has_header()</code>)作为<code>Reader</code>的<code>dialect</code>参数。那可能不是你想做的。相反,您需要<code>dialect</code>和<code>has_header</code>的单独变量:</p>
^{pr2}$