10-3 访客:编写一个程序,提示用户输入其名字;用户作出响应后,将其名字写入到文件guest.txt 中。

编写Python代码:

 username = input("Please enter your name: ")
filename = 'guest.txt' with open(filename, 'w') as name:
name.write(username)

10-4 访客名单:编写一个while 循环,提示用户输入其名字。用户输入其名字后,在屏幕上打印一句问候语,并将一条访问记录添加到文件guest_book.txt 中。确保这个文件中的每条记录都独占一行。

 filename = 'guest_book.txt'
print("Enter 'q' when you are finished. ") while True:
username = input("What's your name? ")
if username == 'q':
break
else:
with open(filename, 'a') as name:
name.write(username + "\n")
print("Hi " + username + ",you've been added to the guest book." )

10-5 关于编程的调查:编写一个while 循环,询问用户为何喜欢编程。每当用户输入一个原因后,都将其添加到一个存储所有原因的文件中。

 filename='programming_poll.txt'
responses = []
while True:
response = input("\nWhy do you like programming? ")
responses.append(response) continue_poll = input("Would you like to let someone else respond? (y/n)")
if continue_poll == 'n':
break with open(filename, 'a') as f:
for response in responses:
f.write(response + '\n')

调用open()方法时,需要提供两个实参:文件名(file)和模式(mode)

'w'表示写入模式,若要写入的文件不存在,则会自动创建,若要写入的文件已经存在,Python在返回文件对象之前会清空该文件。

'r'表示只读模式,若不传递mode参数,默认会以只读'r'打开文件

'a'表示附加模式,若要操作的文件不存在,则会自动创建,若文件已经存在,则会在末尾追加要写入的内容。

Python学习笔记之将数据写入到文件中的更多相关文章

  1. Python:将爬取的网页数据写入Excel文件中

    Python:将爬取的网页数据写入Excel文件中 通过网络爬虫爬取信息后,我们一般是将内容存入txt文件或者数据库中,也可以写入Excel文件中,这里介绍关于使用Excel文件保存爬取到的网页数据的 ...

  2. PHP将数据写入指定文件中

    首先创建一个空的txt文件,这里我们创建了一个1.txt的空文件. 第一种方法:fwrite函数 <?php $file=fopen('1.txt','rb+'); var_dump(fwrit ...

  3. flink---实时项目--day01--1. openrestry的安装 2. 使用nginx+lua将日志数据写入指定文件中 3. 使用flume将本地磁盘中的日志数据采集到的kafka中去

    1. openrestry的安装 OpenResty = Nginx + Lua,是⼀一个增强的Nginx,可以编写lua脚本实现⾮非常灵活的逻辑 (1)安装开发库依赖 yum install -y ...

  4. Python学习笔记_Chapter 4数据保存到文件

    1. What For 将基于内存的数据存储到磁盘上,达到持续存储. 2. HOW 方法一: 将数据写到文件中 常规的处理方式 #file.x被打开的文件,model打开文件的方式 out=open( ...

  5. perl 通过<<和文件句柄将数据写入到文件中去

    可以通过文件句柄和<<运算符将文件内容写入到文件中去 #!usr/bin/perl -W use strict; use Spreadsheet::ParseExcel; use utf8 ...

  6. Python学习笔记_Python向Excel写入数据

    实验环境 1.OS:Win 10 64位 2.Python 3.7 3.如果没有安装xlwt库,则安装:pip install xlwt 下面是从网上找到的一段代码,网上这段代码,看首行注释行,是在L ...

  7. Python学习笔记_从CSV读取数据写入Excel文件中

    本示例特点: 1.读取CSV,写入Excel 2.读取CSV里具体行.具体列,具体行列的值 一.系统环境 1. OS:Win10 64位英文版 2. Python 3.7 3. 使用第三方库:csv. ...

  8. Python学习笔记五(读取提取写入文件)

    #Python打开读取一个文件内容,然后写入一个新的文件中,并对某些字段进行提取,写入新的字段的脚本,与大家共同学习. import os import re def get_filelist(dir ...

  9. 吴裕雄--天生自然python学习笔记:WEB数据抓取与分析

    Web 数据抓取技术具有非常巨大的应用需求及价值, 用 Python 在网页上收集数据,不仅抓取数据的操作简单, 而且其数据分析功能也十分强大. 通过 Python 的时lib 组件中的 urlpar ...

随机推荐

  1. linux设备驱动程序-设备树(2)-device_node转换成platform_device

    设备树处理之--device_node转换成platform_device 以下讨论基于linux4.14,arm平台 platform device 设备树的产生就是为了替代driver中过多的pl ...

  2. 个人第2次作业:熟悉使用Git工具

    GIT地址 https://github.com/dxg1999 GIT用户名 dxg1999 学号后五位 62317 个人博客 我的博客 作业链接 作业内容 项目作业的整个过程 作业背景 阿超家里的 ...

  3. LOJ 3184: 「CEOI2018」斐波那契表示法

    题目传送门:LOJ #3184. 题意简述: 题目说得很清楚了. 题解: 首先需要了解「斐波那契数系」为何物. 按照题目中定义的斐波那契数列 \(F_n\),可以证明,每个非负整数 \(n\) 都能够 ...

  4. 第13节-BLE协议L2CAP层

    学习资料:官方手册 Vol 3: Core System Package [Host volume] Part A: Logical Link Control and Adaptation Proto ...

  5. 201871010134-周英杰《面向对象程序设计(java)》第十四周学习总结

    项目 内容 这个作业属于哪个课程 <https://home.cnblogs.com/u/nwnu-daizh/> 这个作业的要求在哪里 <https://www.cnblogs.c ...

  6. 15-numpy笔记-莫烦pandas-3

    代码 import pandas as pd import numpy as np dates = pd.date_range('20130101', periods=6) df=pd.DataFra ...

  7. python 并行处理数据

    来源:https://blog.csdn.net/weixin_42001089/article/details/88843152 import multiprocessing import time ...

  8. Spring Boot 引入外部yml配置文件

    当需要在springboot中引用其他的yml文件时,需要在application.yml里配置 spring:     profiles:         include: email,xmyb   ...

  9. async-profiler 容器使用常见问题

    Failed to inject profiler log Failed to inject profiler into 1830 linux-vdso.so.1 => (0x00007ffdf ...

  10. nexus php composer 私服搭建

    nexus 社区也提供了php composer 私服(当前还在开发中,还没有ga),测试使用构建好的docker 镜像 环境准备 docker-compose 文件 version: "3 ...