python中如何输出引号
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中如何输出引号相关的知识,希望对你有一定的参考价值。
RT
如何用.:print输出引号(',")或其他特殊符号??
使用转义符输出。
如>>>print ( ' \\" ')可打印双引号,>>>print ( " \\' " ) 可打印单引号。
若要成对打印,可通过>>>print ( ' \\"\\'' ')或>>>print ( ' \\"\\" ' )实现。
所有的ASCII码都可以用“\\”加数字(一般是8进制数字)来表示。
而C中定义了一些字母前加"\\"来表示常见的那些不能显示的ASCII字符,如\\0,\\t,\\n等,就称为转义字符,因为后面的字符,都不是它本来的ASCII字符意思了。
扩展资料:
其他可通过转义符输出的字符:
参考资料:百度百科-转义符
参考技术A 像换行这些需要转义的符号,需要在前面添加转义符\,比如print "\n\r"
如果输出双引号,可以转义,也可以用单引号把双引号包含起来
print "\""
print '"'
输出单引号,同理可以用双引号将单引号包含起来
print "'"本回答被提问者采纳 参考技术B 用s=r''' '''括起来就好(成对的三个单引号或双引号都行) 参考技术C print '"',"'","\n","\t"
如何在 Python csv writer 中将单引号替换为双引号?
【中文标题】如何在 Python csv writer 中将单引号替换为双引号?【英文标题】:How to replace a single quote to double quote in Python csv writer? 【发布时间】:2016-02-09 13:34:12 【问题描述】:我一直在尝试使用 python 输出一个 csv 文件。没有引号和数字就可以正常工作。示例:
代码:
for n in G:
centrality = nx.degree_centrality(G)
betweeness = nx.betweenness_centrality(G)
edges = nx.edges(G, n)
csv_out.writerow([n, str(edges).replace("'",""), round(centrality[n],2), round(betweeness[n],2)])
输出:
24,"[(24, 31), (24, 64)]",0.02,0.04
25,"[(25, 77)]",0.01,0.0
26,"[(26, 49)]",0.01,0.0
27,"[(27, 75), (27, 79)]",0.02,0.02
20,"[(20, 9), (20, 84), (20, 40)]",0.03,0.03
21,"[(21, 77), (21, 64)]",0.02,0.02
我还需要能够输入字符串数据。示例:
Automation Specialist I,"[(Automation Specialist I, Data Coordiator)]",0.01,0.0
Community Outreach Specialist,"[(Community Outreach Specialist, Librarian)]",0.01,0.0
Research Assistant III,"[(Research Assistant III, Senior Quality Engineer)]",0.01,0.0
Cost Accountant,"[(Cost Accountant, Structural Engineer)]",0.01,0.0
Data Coordiator,"[(Data Coordiator, Technical Writer), (Data Coordiator, Automation Specialist I)]",0.02,0.01
但是我需要一个引用的值才能在前端处理它。像这样:
Automation Specialist I,"[("Automation Specialist I", "Data Coordiator")]",0.01,0.0
Community Outreach Specialist,"[("Community Outreach Specialist", "Librarian")]",0.01,0.0
Research Assistant III,"[("Research Assistant III", "Senior Quality Engineer")]",0.01,0.0
Cost Accountant,"[("Cost Accountant", "Structural Engineer")]",0.01,0.0
Data Coordiator,"[("Data Coordiator", "Technical Writer"), ("Data Coordiator", "Automation Specialist I")]",0.02,0.01
所以我使用了下面的代码:
csv_out.writerow([n, str(edges).replace("'",'"'), round(centrality[n],2), round(betweeness[n],2)])
但是,结果是这样的:
对于数字:
24,"[(""24"", ""31""), (""24"", ""64"")]",0.02,0.04
25,"[(""25"", ""77"")]",0.01,0.0
26,"[(""26"", ""49"")]",0.01,0.0
对于字符串:
Automation Specialist I,"[(""Automation Specialist I"", ""Data Coordiator"")]",0.01,0.0
Community Outreach Specialist,"[(""Community Outreach Specialist"", ""Librarian")]"",0.01,0.0
我还尝试用撇号替换 out 引号(列表之外),但没有成功。我也尝试使用下面的代码,但它给出的结果与上面相同。
str(edges).replace("'",'"').replace('""','"')
我应该怎么做才能得到我想要的结果? 我计划在之后过滤输入的类型,以便仅在为字符串时应用该技术。因为如果是数字,理想的输出就是第一个例子。当我使用最后一个时,我的前端代码处理文本和数字。但是当我使用第一个示例时,它不适用于文本,仅适用于数字。
你能帮帮我吗? 如何将撇号替换为引号? 非常感谢。
【问题讨论】:
quotechar
option 应该给你你想要的。您不应该尝试自己修改 CSV 文件 - 格式看起来很简单,但要正确执行实际上相当棘手。 (csv_out
对象的定义是相关的,把它带入问题对你有好处。)
如果你不尝试替换任何东西,你能告诉我们这条线的样子吗?我想看看未格式化的文本是什么样的......我也对这个 str(edges).replace("'",'"').replace('""','"') 感到困惑你为什么做" ' " 以及 ' " ' 和 ' "" ' .... 你为什么使用不同的格式?
写“[(""Automation Specialist I"", ""Data Coordinator"")]"有什么问题?引号字符应该在原始 CSV 格式中加倍。使用像样的 CSV 解析器(如 Excel)读取此 CSV 将生成字符串 [("Automation Specialist I", "Data Coordinator")],这对您的前端来说似乎没问题。
【参考方案1】:
我最终使用了以下代码:
if(is_number(n) == True):
edges_w = str(edges).replace("'","")
else:
edges_w = str(edges).replace("'",'"')
我使用了一个函数来检查它是否是一个数字,然后根据这个函数的返回值替换了str。我考虑过@Stein cmets 来使用这个解决方案。
【讨论】:
如果字符串中间有'
引号,比如撇号怎么办。【参考方案2】:
尝试用反斜杠转义双引号。例如
str(edges).replace("'","\"")
【讨论】:
这与 Jaqueline 所做的完全一样,只是风格问题,python 似乎更喜欢 Jaqueline 的风格而不是你的风格:在 python 提示符上输入"\""
,python 将其回显为 @ 987654323@以上是关于python中如何输出引号的主要内容,如果未能解决你的问题,请参考以下文章
如何在单行代码中删除 python 3 中类列表中的 [ , ] 和单引号? [关闭]