簡體   English   中英

Python configparser 從一個部分獲取所有內容並寫入新文件

[英]Python configparser get all from a section and write to new file

如何使用 python 配置解析器獲取單個部分下的每個條目並寫入新文件而不實際指定每個條目

例如,我將如何在“測試”部分下獲取所有內容並寫入新文件而不使用 config.get 並列出每個條目?

配置文件

[testing]
test1=test23452
test2=test45235
test3=test54524
[donotneed]
something1=something
something2=somethingelse

我已經嘗試了以下只是為了測試目的

config = ConfigParser.ConfigParser()
    config.read(configFilePath)
    testing = {k:v for k,v in config.items('testing')}
    for x in testing:
        print (x)

但它只打印以下內容

test1
test3
test2

而不是該部分中的所有內容,我需要它給我

test1=test23452
test2=test45235
test3=test54524

for x in testing只會解析字典testing的鍵。

你需要:

for x in testing.items():
  print x[0] + '=' + x[1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM