简体   繁体   English

从CSV文件进行Python编辑

[英]Python editing from CSV file

I have a CSV file which I am writing to in Python with the line: 我有一个CSV文件,我正在用Python编写此行:

writeLine = username + "," + password + "," + dob + "," + artist + "," + genre + "," + song + "\n"

Which outputs the following in its own column A - F: 在其自己的A-F列中输出以下内容:

Tom     Tom123    19081991    Queen   Rock    Dont Stop

My question is, how do I read just a particular column and edit that word? 我的问题是,如何只阅读特定的列并编辑该词? (Say I want to edit just the word 'Queen' to something else) I can overwrite the whole line, but placing only into that column i'm a tad stuck on. (假设我只想将“ Queen”一词编辑为其他字词),我可以覆盖整行,但仅放在该列中,我有点犹豫。

Full code is here if you wish to have a gander for full context if I have not made myself clear: 如果您希望对上下文有个完整的了解,请在此处查看完整代码:

pastebin com / 6hDDBDx7 (.s removed) pastebin com / 6hDDBDx7(.s已删除)

You can do smth like this: 您可以这样做:

splitted_line = line.split(',')
splitted_line[number_of_artist_column] = new_artist
new_line = ','.join(splitted_line)

I think, it will be not bad in terms of code beauty 我认为,就代码美而言,这还不错

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

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