简体   繁体   English

如何在 Python csv writer 中将单引号替换为双引号?

[英]How to replace a single quote to double quote in Python csv writer?

I've been trying to output a csv file using python.我一直在尝试使用 python 输出一个 csv 文件。 It's working fine without quotation marks and numbers.它在没有引号和数字的情况下工作正常。 Example:例子:

code:代码:

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)])

output:输出:

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

I need to be able to input also string data.我还需要能够输入字符串数据。 Example:例子:

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

However I need a quoted value in order to process it on the front-end.但是我需要一个引用的值才能在前端处理它。 Like this:像这样:

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

So I used the code below:所以我使用了下面的代码:

csv_out.writerow([n, str(edges).replace("'",'"'), round(centrality[n],2), round(betweeness[n],2)])

However, the result is this:然而,结果是这样的:

for numbers:对于数字:

24,"[(""24"", ""31""), (""24"", ""64"")]",0.02,0.04
25,"[(""25"", ""77"")]",0.01,0.0
26,"[(""26"", ""49"")]",0.01,0.0

for string:对于字符串:

Automation Specialist I,"[(""Automation Specialist I"", ""Data Coordiator"")]",0.01,0.0
Community Outreach Specialist,"[(""Community Outreach Specialist"", ""Librarian")]"",0.01,0.0

I've also tried to replace the out quotation mark (outside of the list) with an apostrophe, without success.我还尝试用撇号替换 out 引号(列表之外),但没有成功。 I tried also to use the code below, but it gives the same result as above.我也尝试使用下面的代码,但它给出了与上面相同的结果。

str(edges).replace("'",'"').replace('""','"')

What should I do in order to get the result I want?我该怎么做才能得到我想要的结果? I plan to filter the type of the input afterwards in order to apply the technique only if is string.我计划在之后过滤输入的类型,以便仅当是字符串时才应用该技术。 Because if it's a number, the ideal output is the first example.因为如果它是一个数字,那么理想的输出就是第一个例子。 When I use the last one, my front-end code process text and numbers.当我使用最后一个时,我的前端代码处理文本和数字。 But when I use the first example, it doesn't works for text, only for numbers.但是当我使用第一个示例时,它不适用于文本,仅适用于数字。

Can you help me, please?你能帮我吗? How can I replace the apostrophe to quotation mark?如何将撇号替换为引号? Thank you very much.非常感谢。

Try to escape the double quotes with a backslash.尝试用反斜杠转义双引号。 eg例如

str(edges).replace("'","\"")

I've ended up using the code below:我最终使用了以下代码:

if(is_number(n) == True):
    edges_w = str(edges).replace("'","")
else:
    edges_w = str(edges).replace("'",'"')

I've used a function to check if it's a number and then replaced the str according to this function return value.我使用了一个函数来检查它是否是一个数字,然后根据这个函数的返回值替换了 str 。 I've considered @Stein comments to use this solution.我已经考虑过@Stein 评论来使用这个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM