程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python reads and modifies the contents of CSV and INI files

編輯:Python

python Read and modify csv Data in

Write data

with open('DataSavePathSet.csv', 'w') as myFile:
myFile.writelines(self.DataSavePath)

Reading data

with open('DataSavePathSet.csv', 'r') as csvfile:
self.DataSavePath =csvfile.readlines()[0]

 

 python Read and modify ini The content in

Write address

curpath = os.path.dirname(os.path.realpath(__file__))
cfgpath = os.path.join(curpath, "cfg.ini")
conf = configparser.ConfigParser()
conf.read(cfgpath, encoding="utf-8")
# Add one select
conf.add_section("DataSaveCfg")
# Go to select add to key and value
conf.set("DataSaveCfg", "path", self.DataSavePath)
conf.write(open(cfgpath, "w+", encoding="utf-8")) # w+ Pattern 
 

Read the address

conf = configparser.ConfigParser()
# read ini file
conf.read("cfg.ini", encoding="utf-8")
Refreshpath = conf.get("DataSaveCfg", "path")

python use DataFrame Read and save excel

read

import pandas as pd
e = 'data.xlsx'
data = pd.read_excel(e)

Write

data.to_excel('data.xlsx',index=None) # Will overwrite the original file 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved