擅长:python、mysql、java
<p>csv模块可以轻松处理这样的作业:</p>
<pre><code># Contents of input_file.csv
# 4;1;"COFFEE; COMPANY";4
# 3;2;SALVATION ARMY;4
import csv
input_file = 'input_file.csv' # Contents as shown in your question.
with open(input_file, 'r', newline='') as inp:
for row in csv.reader(inp, delimiter=';'):
row[2] = row[2].replace(';', '') # Removed embedded ';' chars.
# If you don't care about what's in the column, use the following instead:
# row[2] = 'xxxxxxxxxxx' # Value not needed.
print(';'.join(row))
</code></pre>
<p>打印输出:</p>
^{pr2}$
<p><strong>后续问题</strong>:如何将此数据写入新的csv文件?在</p>
<pre><code>import csv
input_file = 'input_file.csv' # Contents as shown in your question.
output_file = 'output_file.csv'
with open(input_file, 'r', newline='') as inp, \
open(output_file, 'w', newline='') as outp:
writer= csv.writer(outp, delimiter=';')
for row in csv.reader(inp, delimiter=';'):
row[2] = row[2].replace(';', '') # Removed embedded ';' chars.
writer.writerow(row)
</code></pre>