configparser.ConfigParser.writer()

博客介绍了使用ConfigParser将配置表示写入指定文本模式文件对象的操作,还提到若指定文件不存在,ConfigParser实例将包含空数据集,应用若需从文件加载初始值,应在读取可选文件前用read_file()加载所需文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    ①
    write(fileobject, space_around_delimiters=True)

Write a representation of the configuration to the specified file object,which must be opened in text mode(accepting strings),This representation can ba parsed by a future read() call.if space_around_delimiters is true,delimiters between keys an values are surrounded by spaces.

config = configparser.Configparser()
must be opened in text mode (accept strings):只能使用text模式打开的文件才能被写入,with open('users.pkl', 'w') as f:
config.writer(f)

②
read(filenames, encoding=None)
            If filenames is a string or path-like object, it is treated as a single filename. If a file named in filenames cannot be opened, that file will be ignored. This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user’s home directory, and some system-wide directory), and all existing configuration files in the list will be read.

If none of the named files exist, the ConfigParser instance will contain an empty dataset. An application which requires initial values to be loaded from a file should load the required file or files using read_file() before calling read() for any optional files:

    import configparser, os

config = configparser.ConfigParser()
config.read_file(open('defaults.cfg'))

转载于:https://blog.51cto.com/13118411/2167133

import csv from datetime import date import os import random import configparser import requests from lxml import etree from fake_useragent import UserAgent def configInfo(): '''Reads the configuration file "config.ini" to get the file path of the csv file that stores the vocabulary and the number of appearances of a word presented. Return in a tuple with these two values''' config = configparser.ConfigParser() config.read(os.path.join(os.path.dirname(__file__), 'config.ini')) #reads the configuration file return (config.get('FILES', 'the_file'),int(config.get('APPEARANCES', 'appearances'))) #return the two vales def theDate(datePrompt, sep = " "): '''Used for acquiring a date from the user. Different prompt could be presented by modifying the argument "datePrompt", which must be a string. If there is no input, return the date of today. The default input format is "yyyy mm dd". Different separator may be specified by the argument sep.''' try: #return a date object if the input format is correct return (lambda x: date(*map(int, x.split(sep))) if x != "" else date.today())(input(datePrompt)) except (TypeError, ValueError): #if format not correct, the user will be asked again print("The date format is not correct!\n") return theDate(datePrompt, sep) def inputWords(inputWordsPrompt, sep = ","): '''Used to acquire the words that the user wants to store, and return a list with the words. The prompt and the separator can be modified.''' return [word.strip() for word in input(inputWordsPrompt).lower().split(sep)] def inputDateAndWords(inputDate, inputWordList): '''Return the full list of a date and the words.''' return [inputDate] + inputWordList def writeWords(dateWordsList, filePath): '''Writes the list with date and words to the csv file.''' with open(filePath, 'a', newline = '') as file: csv.writer(file).writerow(dateWordsList) def file2Dict(filePath): '''Re
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值