how to convert a json type data into csv formatHow can I convert JSON to CSV?How to get a function name as a string in Python?How to determine a Python variable's type?Proper way to declare custom exceptions in modern Python?Why can't Python parse this JSON data?What are “named tuples” in Python?Importing files from different folderHow to read a text file into a string variable and strip newlines?How to change a string into uppercaseHow do I write JSON data to a file?Using MRjob for natural join
Does Google Maps take into account hills/inclines for route times?
Are there any double stars that I can actually see orbit each other?
3D-Plot with an inequality condition for parameter values
What is the English equivalent of 干物女 (dried fish woman)?
What exactly is the Tension force?
Published paper containing well-known results
Why can't air tickets just accept only the passport number without any names?
Won 50K! Now what should I do with it
Is a public company able to check out who owns its shares in very detailed format?
School House Points (Python + SQLite)
Can I activate an iPhone without an Apple ID?
How do I define this subset using mathematical notation?
How can I legally visit the United States Minor Outlying Islands in the Pacific?
Cubic programming and beyond?
Should you avoid redundant information after dialogue?
Was adding milk to tea started to reduce employee tea break time?
Meaning of slash chord without anything left of the slash
Can I capture stereo IQ signals from WebSDR?
HackerRank: Electronics Shop
Integral clarification
Can a pizza stone be fixed after soap has been used to clean it?
What caused Windows ME's terrible reputation?
Do native speakers use ZVE or CPU?
Is it rude to tell recruiters I would only change jobs for a better salary?
how to convert a json type data into csv format
How can I convert JSON to CSV?How to get a function name as a string in Python?How to determine a Python variable's type?Proper way to declare custom exceptions in modern Python?Why can't Python parse this JSON data?What are “named tuples” in Python?Importing files from different folderHow to read a text file into a string variable and strip newlines?How to change a string into uppercaseHow do I write JSON data to a file?Using MRjob for natural join
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Any suggestion on how to convert data as follows into csv file in python:
[
'name': 'a',
'url': 'url1'
,
'name': 'b',
'url': 'url2'
,
'name': 'c',
'url': 'url3'
]
thank you.
python format-conversion
add a comment |
Any suggestion on how to convert data as follows into csv file in python:
[
'name': 'a',
'url': 'url1'
,
'name': 'b',
'url': 'url2'
,
'name': 'c',
'url': 'url3'
]
thank you.
python format-conversion
3
what have you tried ?
– Kunal Mukherjee
Mar 26 at 6:38
5
Possible duplicate of How can I convert JSON to CSV?
– Maged Saeed
Mar 26 at 6:39
Well, I'd say it depends on what assumptions you can make. Can you assume that the input will always consist of a list of dicts that always contain exactly both a 'name' key and a 'url' key? If so, it's pretty simple. Just iterate over the list, and extract and print the values of each key in the dict, seprated by a comma, and ended with a newline. If you have to allow for arbitrary dicts containing unconstrained key values, your task is much more difficult. There are libraries to do this for you. - Ah ha! I just saw the comment about the existing SO post. There's a lot there to chew on.
– Steve
Mar 26 at 6:41
1
Actually, that post isn't quite a duplicate, because the input string you present isn't JSON. It isn't a proper Python constant either. That doesn't mean it isn't a valid format. It just means you'll have more trouble finding a canned routine to read it. It would be valid Python, if each of the keys and values in that input were previously defined variables. To be JSON, you need to put double quotes around all of the keys and values in the maps. Same to make it a valid Python structure without defining a bunch of variables.
– Steve
Mar 26 at 6:49
add a comment |
Any suggestion on how to convert data as follows into csv file in python:
[
'name': 'a',
'url': 'url1'
,
'name': 'b',
'url': 'url2'
,
'name': 'c',
'url': 'url3'
]
thank you.
python format-conversion
Any suggestion on how to convert data as follows into csv file in python:
[
'name': 'a',
'url': 'url1'
,
'name': 'b',
'url': 'url2'
,
'name': 'c',
'url': 'url3'
]
thank you.
python format-conversion
python format-conversion
edited Mar 26 at 7:44
Kunal Mukherjee
3,2943 gold badges12 silver badges31 bronze badges
3,2943 gold badges12 silver badges31 bronze badges
asked Mar 26 at 6:32
pythonerpythoner
1
1
3
what have you tried ?
– Kunal Mukherjee
Mar 26 at 6:38
5
Possible duplicate of How can I convert JSON to CSV?
– Maged Saeed
Mar 26 at 6:39
Well, I'd say it depends on what assumptions you can make. Can you assume that the input will always consist of a list of dicts that always contain exactly both a 'name' key and a 'url' key? If so, it's pretty simple. Just iterate over the list, and extract and print the values of each key in the dict, seprated by a comma, and ended with a newline. If you have to allow for arbitrary dicts containing unconstrained key values, your task is much more difficult. There are libraries to do this for you. - Ah ha! I just saw the comment about the existing SO post. There's a lot there to chew on.
– Steve
Mar 26 at 6:41
1
Actually, that post isn't quite a duplicate, because the input string you present isn't JSON. It isn't a proper Python constant either. That doesn't mean it isn't a valid format. It just means you'll have more trouble finding a canned routine to read it. It would be valid Python, if each of the keys and values in that input were previously defined variables. To be JSON, you need to put double quotes around all of the keys and values in the maps. Same to make it a valid Python structure without defining a bunch of variables.
– Steve
Mar 26 at 6:49
add a comment |
3
what have you tried ?
– Kunal Mukherjee
Mar 26 at 6:38
5
Possible duplicate of How can I convert JSON to CSV?
– Maged Saeed
Mar 26 at 6:39
Well, I'd say it depends on what assumptions you can make. Can you assume that the input will always consist of a list of dicts that always contain exactly both a 'name' key and a 'url' key? If so, it's pretty simple. Just iterate over the list, and extract and print the values of each key in the dict, seprated by a comma, and ended with a newline. If you have to allow for arbitrary dicts containing unconstrained key values, your task is much more difficult. There are libraries to do this for you. - Ah ha! I just saw the comment about the existing SO post. There's a lot there to chew on.
– Steve
Mar 26 at 6:41
1
Actually, that post isn't quite a duplicate, because the input string you present isn't JSON. It isn't a proper Python constant either. That doesn't mean it isn't a valid format. It just means you'll have more trouble finding a canned routine to read it. It would be valid Python, if each of the keys and values in that input were previously defined variables. To be JSON, you need to put double quotes around all of the keys and values in the maps. Same to make it a valid Python structure without defining a bunch of variables.
– Steve
Mar 26 at 6:49
3
3
what have you tried ?
– Kunal Mukherjee
Mar 26 at 6:38
what have you tried ?
– Kunal Mukherjee
Mar 26 at 6:38
5
5
Possible duplicate of How can I convert JSON to CSV?
– Maged Saeed
Mar 26 at 6:39
Possible duplicate of How can I convert JSON to CSV?
– Maged Saeed
Mar 26 at 6:39
Well, I'd say it depends on what assumptions you can make. Can you assume that the input will always consist of a list of dicts that always contain exactly both a 'name' key and a 'url' key? If so, it's pretty simple. Just iterate over the list, and extract and print the values of each key in the dict, seprated by a comma, and ended with a newline. If you have to allow for arbitrary dicts containing unconstrained key values, your task is much more difficult. There are libraries to do this for you. - Ah ha! I just saw the comment about the existing SO post. There's a lot there to chew on.
– Steve
Mar 26 at 6:41
Well, I'd say it depends on what assumptions you can make. Can you assume that the input will always consist of a list of dicts that always contain exactly both a 'name' key and a 'url' key? If so, it's pretty simple. Just iterate over the list, and extract and print the values of each key in the dict, seprated by a comma, and ended with a newline. If you have to allow for arbitrary dicts containing unconstrained key values, your task is much more difficult. There are libraries to do this for you. - Ah ha! I just saw the comment about the existing SO post. There's a lot there to chew on.
– Steve
Mar 26 at 6:41
1
1
Actually, that post isn't quite a duplicate, because the input string you present isn't JSON. It isn't a proper Python constant either. That doesn't mean it isn't a valid format. It just means you'll have more trouble finding a canned routine to read it. It would be valid Python, if each of the keys and values in that input were previously defined variables. To be JSON, you need to put double quotes around all of the keys and values in the maps. Same to make it a valid Python structure without defining a bunch of variables.
– Steve
Mar 26 at 6:49
Actually, that post isn't quite a duplicate, because the input string you present isn't JSON. It isn't a proper Python constant either. That doesn't mean it isn't a valid format. It just means you'll have more trouble finding a canned routine to read it. It would be valid Python, if each of the keys and values in that input were previously defined variables. To be JSON, you need to put double quotes around all of the keys and values in the maps. Same to make it a valid Python structure without defining a bunch of variables.
– Steve
Mar 26 at 6:49
add a comment |
1 Answer
1
active
oldest
votes
It isn't as simple as what I said in my first comment. When I said that, I didn't realize that your input string isn't in any standard format that's easy to read with a JSON library or by interpreting as Python code.
Here's a very restrictive answer, that will only work on input strings that are of that very specific form:
data = "[name:a,url:url1,name:b,url:url2,name:c,url:url3]"
entries = re.findall("([^:]+):([^,]+),([^:]+):([^]+)", data)
with open("/tmp/output.csv", "w") as f:
f.write("Name,Urln")
for entry in entries:
f.write(entry[1] + ',' + entry[3] + 'n')
Resulting file contents:
Name,Url
a,url1
b,url2
c,url3
Hi,Steve. Thank you very much. It really works!
– pythoner
Mar 26 at 7:10
Cool. Can you "check" the answer please if you like it :)
– Steve
Mar 26 at 7:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351057%2fhow-to-convert-a-json-type-data-into-csv-format%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It isn't as simple as what I said in my first comment. When I said that, I didn't realize that your input string isn't in any standard format that's easy to read with a JSON library or by interpreting as Python code.
Here's a very restrictive answer, that will only work on input strings that are of that very specific form:
data = "[name:a,url:url1,name:b,url:url2,name:c,url:url3]"
entries = re.findall("([^:]+):([^,]+),([^:]+):([^]+)", data)
with open("/tmp/output.csv", "w") as f:
f.write("Name,Urln")
for entry in entries:
f.write(entry[1] + ',' + entry[3] + 'n')
Resulting file contents:
Name,Url
a,url1
b,url2
c,url3
Hi,Steve. Thank you very much. It really works!
– pythoner
Mar 26 at 7:10
Cool. Can you "check" the answer please if you like it :)
– Steve
Mar 26 at 7:38
add a comment |
It isn't as simple as what I said in my first comment. When I said that, I didn't realize that your input string isn't in any standard format that's easy to read with a JSON library or by interpreting as Python code.
Here's a very restrictive answer, that will only work on input strings that are of that very specific form:
data = "[name:a,url:url1,name:b,url:url2,name:c,url:url3]"
entries = re.findall("([^:]+):([^,]+),([^:]+):([^]+)", data)
with open("/tmp/output.csv", "w") as f:
f.write("Name,Urln")
for entry in entries:
f.write(entry[1] + ',' + entry[3] + 'n')
Resulting file contents:
Name,Url
a,url1
b,url2
c,url3
Hi,Steve. Thank you very much. It really works!
– pythoner
Mar 26 at 7:10
Cool. Can you "check" the answer please if you like it :)
– Steve
Mar 26 at 7:38
add a comment |
It isn't as simple as what I said in my first comment. When I said that, I didn't realize that your input string isn't in any standard format that's easy to read with a JSON library or by interpreting as Python code.
Here's a very restrictive answer, that will only work on input strings that are of that very specific form:
data = "[name:a,url:url1,name:b,url:url2,name:c,url:url3]"
entries = re.findall("([^:]+):([^,]+),([^:]+):([^]+)", data)
with open("/tmp/output.csv", "w") as f:
f.write("Name,Urln")
for entry in entries:
f.write(entry[1] + ',' + entry[3] + 'n')
Resulting file contents:
Name,Url
a,url1
b,url2
c,url3
It isn't as simple as what I said in my first comment. When I said that, I didn't realize that your input string isn't in any standard format that's easy to read with a JSON library or by interpreting as Python code.
Here's a very restrictive answer, that will only work on input strings that are of that very specific form:
data = "[name:a,url:url1,name:b,url:url2,name:c,url:url3]"
entries = re.findall("([^:]+):([^,]+),([^:]+):([^]+)", data)
with open("/tmp/output.csv", "w") as f:
f.write("Name,Urln")
for entry in entries:
f.write(entry[1] + ',' + entry[3] + 'n')
Resulting file contents:
Name,Url
a,url1
b,url2
c,url3
answered Mar 26 at 7:02
SteveSteve
4,7681 gold badge8 silver badges31 bronze badges
4,7681 gold badge8 silver badges31 bronze badges
Hi,Steve. Thank you very much. It really works!
– pythoner
Mar 26 at 7:10
Cool. Can you "check" the answer please if you like it :)
– Steve
Mar 26 at 7:38
add a comment |
Hi,Steve. Thank you very much. It really works!
– pythoner
Mar 26 at 7:10
Cool. Can you "check" the answer please if you like it :)
– Steve
Mar 26 at 7:38
Hi,Steve. Thank you very much. It really works!
– pythoner
Mar 26 at 7:10
Hi,Steve. Thank you very much. It really works!
– pythoner
Mar 26 at 7:10
Cool. Can you "check" the answer please if you like it :)
– Steve
Mar 26 at 7:38
Cool. Can you "check" the answer please if you like it :)
– Steve
Mar 26 at 7:38
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351057%2fhow-to-convert-a-json-type-data-into-csv-format%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
what have you tried ?
– Kunal Mukherjee
Mar 26 at 6:38
5
Possible duplicate of How can I convert JSON to CSV?
– Maged Saeed
Mar 26 at 6:39
Well, I'd say it depends on what assumptions you can make. Can you assume that the input will always consist of a list of dicts that always contain exactly both a 'name' key and a 'url' key? If so, it's pretty simple. Just iterate over the list, and extract and print the values of each key in the dict, seprated by a comma, and ended with a newline. If you have to allow for arbitrary dicts containing unconstrained key values, your task is much more difficult. There are libraries to do this for you. - Ah ha! I just saw the comment about the existing SO post. There's a lot there to chew on.
– Steve
Mar 26 at 6:41
1
Actually, that post isn't quite a duplicate, because the input string you present isn't JSON. It isn't a proper Python constant either. That doesn't mean it isn't a valid format. It just means you'll have more trouble finding a canned routine to read it. It would be valid Python, if each of the keys and values in that input were previously defined variables. To be JSON, you need to put double quotes around all of the keys and values in the maps. Same to make it a valid Python structure without defining a bunch of variables.
– Steve
Mar 26 at 6:49