简体   繁体   English

使用熊猫或csv模块将值写入csv文件中的给定文件

[英]Writing value to given filed in csv file using pandas or csv module

Is there any way you can write value to specific place in given .csv file using pandas or csv module? 有什么方法可以使用pandas或csv模块将值写入给定.csv文件中的特定位置?

I have tried using csv_reader to read the file and find a line which fits my requirements though I couldn't figure out a way to switch value which is in the file to mine. 我尝试使用csv_reader读取文件并找到符合我要求的行,尽管我无法找出一种方法来切换要挖掘的文件中的值。

What I am trying to achieve here is that I have a spreadsheet of names and values. 我要在这里实现的是,我有一个包含名称和值的电子表格。 I am using JSON to update the values from the server and after that I want to update my spreadsheet also. 我正在使用JSON更新服务器中的值,之后我还想更新电子表格。

The latest solution which I came up with was to create separate sheet from which I will get updated data, but this one is not working, though there is no sequence in which the dict is written to the file. 我想出的最新解决方案是创建一个单独的工作表,从该工作表中我可以获取更新的数据,但是这没有用,尽管没有顺序将字典写入文件。

def updateSheet(fileName, aValues):
    with open(fileName+".csv") as workingSheet:
        writer = csv.DictWriter(workingSheet,aValues.keys())
        writer.writeheader()
        writer.writerow(aValues)

I will appreciate any guidance and tips. 我将不胜感激任何指导和技巧。

You can try this way to operate the specified csv file 您可以尝试这种方式来操作指定的csv文件

import pandas as pd  
a = ['one','two','three']  
b = [1,2,3]  
english_column = pd.Series(a, name='english')  
number_column = pd.Series(b, name='number')  
predictions = pd.concat([english_column, number_column], axis=1)   
save = pd.DataFrame({'english':a,'number':b})  
save.to_csv('b.csv',index=False,sep=',')  

在此处输入图片说明

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

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