简体   繁体   English

使用 python 添加列表到 csv 文件中的位置

[英]Using python to add a list to a postion in a csv file

Im rather new to modifying csv files, but I need to add a list as a row to particular position in a file, such as position H. I initially opened a file for reading and then set it to a variable, and for the length of that variable append the ith element of my list to position H of that file or list.我对修改 csv 文件相当陌生,但我需要将列表作为一行添加到文件中的特定 position,例如 position H。我最初打开一个文件进行读取,然后将其设置为一个变量,长度为该变量 append 我列表的第 i 个元素到该文件或列表的 position H。 I'm very confused on executing this though so anyones help would be very well appreciated.不过,我对执行此操作感到非常困惑,因此非常感谢任何人的帮助。

You would read in the CSV as a list of lines and insert the new line and then output the list as a new CSV file.您可以将 CSV 作为行列表读取并插入新行,然后将 output 列表作为新的 CSV 文件插入。

lines = csv.read('my_file.csv')
lines.insert(rowIndex, newRow)
csv.write(lines, 'my_new_file.csv')

That's pseudo-code but that's how it should work.那是伪代码,但它应该是这样工作的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM