简体   繁体   English

如何使用 csv 模块制作 python 代码,该模块在相应的迭代中更改每一行

[英]How can I make a python code with the csv module that change each row at respective iteration

Suppose there is a csv file named 'example.csv':假设有一个名为“example.csv”的 csv 文件:

a, b, c a, b, c

a, b一,乙

a, b一,乙

a, b一,乙

a, b一,乙

I want to change all rows of this csv file to be 'a, b, c' using a Python code with csv module, however suppose that this code is heavier than my computer's computing power so, perhaps, the program may be abruptly interrupted (Probably, the user stop the program).我想使用 csv 模块的 Python 代码将这个 csv 文件的所有行更改为“a,b,c”,但是假设这个代码可能比我的计算机突然中断可能,用户停止程序)。 In this case, the progress of this csv must be saved.在这种情况下,必须保存这个 csv 的进度。 For example, if the code did two iterations and terminated, then the original 'example.csv' or copied version 'copied_example.csv' should be:例如,如果代码进行了两次迭代并终止,那么原始的“example.csv”或复制的版本“copied_example.csv”应该是:

a, b, c a, b, c

a, b, c a, b, c

a, b, c a, b, c

a, b一,乙

a, b一,乙

You could in theory loop through the csv and save changes in every loop.理论上,您可以循环通过 csv 并在每个循环中保存更改。 Instead of using a csv module, you could use normal open() and then write() in a loop.除了使用 csv 模块,您可以使用普通的 open() 然后在循环中使用 write()。

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

相关问题 如何更改此代码以使每个文件都显示进度条并且每个循环都进行迭代? - How can I change this code to make the progress bars appear for each file and the iteration be each loop? 如何从 python 模块代码制作 csv 文件? - How can I make a csv file from a python module code? 如何在新行中制作每个数据 [data to csv]? - How can I make each data in new row [data to csv]? Python-如何更改CSV文件中的行的一部分? - Python-How can i change part of a row in a CSV file? 如何从我的 python 代码中创建单个 Dataframe 代码,为 for 循环的每次迭代生成字典? - How can I create a single Dataframe out of my python code that generates a dictionary for each iteration of a for loop? Python - 如何在每次 FOR 迭代中更改 UUID? - Python - How do I get UUID to change on each FOR iteration? 如何在不重新启动python解释器的情况下对模块进行更改? - How can I make a change to a module without restarting python interpreter? 我怎样才能使这个循环每次迭代增加 1.8? - How can I make this loop increase by 1.8 each iteration? 如何使用 python 将 csv 文件中的每 7 行转换为单行? - How can I transform each 7 row to single row in csv file with python? 使用 Python 中的模块运行 CSV 的每一行 - Running each row of a CSV with a module in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM