Jaqueline Passos
Jaqueline Passos

Reputation: 1389

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

I've been trying to output a csv file using python. 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. 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.

Upvotes: 3

Views: 3944

Answers (2)

Jaqueline Passos
Jaqueline Passos

Reputation: 1389

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. I've considered @Stein comments to use this solution.

Upvotes: 1

John
John

Reputation: 76

Try to escape the double quotes with a backslash. e.g.

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

Upvotes: 1

Related Questions