[英]Editing a csv file with python
Ok so I'm looking to create a program that will interact with an excel spreadsheet.好的,所以我正在寻找创建一个与 Excel 电子表格交互的程序。 The idea that seemed to work the most is converting it to a csv file.
似乎最有效的想法是将其转换为 csv 文件。 I've managed to make a program that prints the data but I want it to edit it and thus change the results in the csv file itself.
我设法制作了一个打印数据的程序,但我希望它对其进行编辑,从而更改 csv 文件本身的结果。
Sorry if it's a bit confusing as my programming skills aren't great.对不起,如果我的编程技巧不是很好,这有点令人困惑。
Heres the code:代码如下:
import csv
with open('wert.csv') as csvfile:
freq=csv.reader(csvfile, delimiter=',')
for row in freq:
print(row[0],row[1],row[2])
If anyone has a better idea on how to make this program work then it would be greatly appreciated.如果有人对如何使该程序工作有更好的想法,我们将不胜感激。
Thanks谢谢
If you csv file is composed of just numbers (floats) or numbers and a header, you can try reading it with:如果您的 csv 文件仅由数字(浮点数)或数字和标题组成,您可以尝试使用以下方法阅读:
import numpy as np
data=np.genfromtxt('name.csv',delimiter=',',skip_header=1)
Then modify your data in python, and save it with:然后在python中修改您的数据,并将其保存为:
data_modified=data**2 #for example
np.savetxt('name_modified.csv',data_modified,delimiter=',',header='whaterverheader,you,want')
You can read the excel file directly using pandas and do the processing directly可以直接使用pandas读取excel文件,直接做处理
import pandas
measured_data = pandas.read_excel(filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.