非常基本的Python问题字符串,格式和转义符

如何解决非常基本的Python问题字符串,格式和转义符

|| 我开始通过在线指南学习Python,而我刚做了一个练习,需要我编写以下脚本:
from sys import argv

script,filename = argv

print \"We\'re going to erase %r.\" % filename
print \"If you don\'t want that,hit CTRL-C (^C).\"
print \"If you do want that,hit RETURN.\"

raw_input(\"?\")

print \"Opening the file...\"
target = open(filename,\'w\')

print \"Truncating the file. Goodbye!\"
target.truncate()

print \"Now I\'m going to ask you for three lines.\"

line1 = raw_input(\"line 1: \")
line2 = raw_input(\"line 2: \")
line3 = raw_input(\"line 3: \")

print \"I\'m going to write these to the file.\"

target.write(line1)
target.write(\"\\n\")
target.write(line2)
target.write(\"\\n\")
target.write(line3)
target.write(\"\\n\")

print \"And finally,we close it.\"
target.close()
我运行正常,但随后指南说: \“此文件中的重复太多。使用字符串,格式和转义符仅通过一个target.write()命令而不是6来打印第1行,第2行和第3行。” 我不确定该怎么做。有人可以帮忙吗?谢谢!     

解决方法

        该指南建议创建一个字符串并将其写出,而不是六次调用
write()
,这似乎是个不错的建议。 您有三个选择。 您可以像这样将字符串并置在一起:
line1 + \"\\n\" + line2 + \"\\n\" + line3 + \"\\n\"
或像这样:
\"\\n\".join(line1,line2,line3) + \"\\n\"
您可以使用旧的字符串格式来做到这一点:
\"%s\\n%s\\n%s\\n\" % (line1,line3)
最后,您可以使用Python 3中使用的较新的字符串格式,也可以从Python 2.6中使用:
\"{0}\\n{1}\\n{2}\\n\".format(line1,line3)
我建议使用最后一种方法,因为当您掌握它时,它是最强大的,它将为您提供:
target.write(\"{0}\\n{1}\\n{2}\\n\".format(line1,line3))
    ,        怎么样
target.write(\'%s \\n %s \\n %s\' % (line1,line3))
    ,        我认为他们希望您使用字符串连接:
target.write(line1 + \"\\n\" + line2 + \"\\n\" + line3 + \"\\n\")
可读性差得多,但您只有一个
target.write()
命令     ,        这需要两行。 它将您要打印的行放在一个变量中,以使其更具可读性
lb = \"\\n\"
allOnOne= line1 + lb + line2 + lb+ line3 + lb 
target.write(allOnOne) 
    ,        我目前正在按照同一门课程学习,发现的解决方案与使用的忍者壁虎相似,不同之处在于,我只使用了到目前为止在课程中所学到的东西。我的看起来像这样:
from sys import argv
script,filename = argv
print \"We\'re going to erase %s.\" % filename
print \"If you don\'t want that,hit CTRL-C (^C).\"
print \"If you do want that,hit RETURN.\"

raw_input(\"?\")

print \"Opening the file...\"
target = open(filename,\'w\')

print \"Truncating the file. Goodbye!\"
target.truncate()

print \"Now I\'m going to ask you for three lines.\"

lines = [raw_input(\"Lines %r :\" % i) for i in range(1,4)]

for line in lines:
    target.write(line + \"\\n\")

print \"And finally,we close it.\"
target.close()
我花了一点时间移动括号并弄清楚格式化程序和循环的放置位置,但是一旦找到它,对我来说就很有意义了。要注意的一件事是我的第一次尝试:
for i in range(1,4):
    lines = raw_input(\"Line %r :\" % i)
最初在运行脚本时似乎可以工作,但是在查看目标文件时,它仅将最后一行(第3行)写入文件。我仍然不清楚为什么会这样。     ,        我也是第一次参加本课程,并且想知道同一件事,这就是我想出的一切,并且使它可以毫无问题地工作。我仍在学习此方法,因此,如果这是不好的形式,请告诉我。这就是我要为我工作的。,
target.write(\"%s \\n%s \\n%s\" % (line1,line3))
    ,        出于该特定指南中“该特定研究钻探”的问题/问题的目的,我相信作者希望...
target.write(\"%s\\n%s\\n%s\\n\" % (line1,line3))
尽管,戴夫·韦伯(Dave Webb)当然也因透彻的学习和教育价值而获得了许多布朗尼点。     ,        坏 原始代码是重复的,粘贴代码很危险(为什么“复制和粘贴”代码很危险?):
print \"Now I\'m going to ask you for three lines.\"

line1 = raw_input(\"line 1: \")
line2 = raw_input(\"line 2: \")
line3 = raw_input(\"line 3: \")

print \"I\'m going to write these to the file.\"

target.write(line1)
target.write(\"\\n\")
target.write(line2)
target.write(\"\\n\")
target.write(line3)
target.write(\"\\n\")
好 短得多,只需更改一个字符即可将其更改为4行以上:
print \"Now I\'m going to ask you for three lines.\"

lines = [raw_input(\"line {i}: \".format(i=i)) for i in range(1,4)]

print \"I\'m going to write these to the file.\"

for line in lines:
    target.write(line+\'\\n\')
    ,        我认为目的是让学生利用上一课中讲授的内容并得出以下解决方案:
print \"Now I\'m going to ask you for three lines.\"

line1 = raw_input(\"line 1: \")
line2 = raw_input(\"line 2: \")
line3 = raw_input(\"line 3: \")

print \"I\'m going to write these to the file.\"

target.write(line1 + \'\\n\' + line2 + \'\\n\' + line3 + \'\\n\')

print \"And finally,we close it.\"
target.close()
    ,        这个怎么样?我使用了for循环。
from sys import argv

script,filename = argv

print(\"We\'re going to erase %r.\" % filename)
print(\"If you don\'t want that,hit CTRL-C (^C).\")
print(\"If you want that,hit RETURN.\")

input(\"?\")

print(\"Opening the file...\")
target = open(filename,\'w\')

print(\"Truncating the file. Goodbye!\")
target.truncate()

print(\"Now I am going to ask you for three lines.\")

line1 = input(\"line 1: \")
line2 = input(\"line 2: \")
line3 = input(\"line 3: \")

print(\"I\'m going to write these to the file.\")

for a in (line1,line3):
    target.write(\"\\n\")

target.close()
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-