Why does my script not print entire contents of json file to csvWriting with Python's built-in .csv moduleWhat does “SyntaxError: Missing parentheses in call to 'print'” mean in Python?Why does “not(True) in [False, True]” return False?Python3 csv writer failing, exiting on error "TypeError: 'newline' is an invalid keyword argument for this functionpython csv module issues with embedded JSON strings (Python + Oracle + CSV + JSON)python : How to allow python to not add quotation marks while changing delimiter in the csv filePython3 Replacing special character from .csv file after convert the same from JSONDelete last (and blank) line from file written by CSV writerAssign csv files to a collection of dictionaries (list) with file name as the keys and file content as the valuesWriting Web-Scrape Elements to csv file with desired formatting
How can I find out about the game world without meta-influencing it?
A flower's head or heart?
Can artificial satellite positions affect tides?
Why are backslashes included in this shell script?
Idiom for 'person who gets violent when drunk"
Nth term of Van Eck Sequence
Must a CPU have a GPU if the motherboard provides a display port (when there isn't any separate video card)?
New Site Design!
Can Mage Hand be used to indirectly trigger an attack?
Would a bit of grease on overhead door cables or bearings cause the springs to break?
Is it ethical to cite a reviewer's papers even if they are rather irrelevant?
Short story about psychologist analyzing demon
What game uses dice with compass point arrows, forbidden signs, explosions, arrows and targeting reticles?
Why did the Death Eaters wait to reopen the Chamber of Secrets?
Dedicated bike GPS computer over smartphone
Opposite of "Concerto Grosso"?
Why is C++ template use not recommended in space/radiated environment?
Optimising matrix generation time
Realistic, logical way for men with medieval-era weaponry to compete with much larger and physically stronger foes
typeid("") != typeid(const char*)
Print the phrase "And she said, 'But that's his.'" using only the alphabet
Loop counter not interpreted as number
What did the 8086 (and 8088) do upon encountering an illegal instruction?
Can I get a photo of an Ancient Arrow?
Why does my script not print entire contents of json file to csv
Writing with Python's built-in .csv moduleWhat does “SyntaxError: Missing parentheses in call to 'print'” mean in Python?Why does “not(True) in [False, True]” return False?Python3 csv writer failing, exiting on error "TypeError: 'newline' is an invalid keyword argument for this functionpython csv module issues with embedded JSON strings (Python + Oracle + CSV + JSON)python : How to allow python to not add quotation marks while changing delimiter in the csv filePython3 Replacing special character from .csv file after convert the same from JSONDelete last (and blank) line from file written by CSV writerAssign csv files to a collection of dictionaries (list) with file name as the keys and file content as the valuesWriting Web-Scrape Elements to csv file with desired formatting
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have some json files that I need to convert to csv.
I have written a very simple script, but the results are unexpected. Instead of having all contents of the json file written to the csv file, I only get the top level name with each letter separated by a comma. Here's the json file contents:
"drivers": ["id": 91907, "groupId": 9039, "vehicleId": 212014918234488, "username": "abrauer",
"id": 134763, "groupId": 9039, "vehicleId": 212014918234742, "username": "abarbosa",
"id": 134764, "groupId": 9039, "vehicleId": 212014918234709, "username": "btoole",
"id": 134766, "groupId": 9039, "vehicleId": 212014918234773, "username": "bheinsohn"]
and the code:
import csv
import json
infile = open("driver.json","r")
outfile = open("driver.csv","w")
writer = csv.writer(outfile)
for row in json.loads(infile.read()):
writer.writerow(row)
The results are as follows:
d,r,i,v,e,r,s. Should I expect to remove {"drivers": from the file before trying to convert?
Update...
I tried removing "drivers": from the file, as well as the trailing '', and the results is just as strange. Now, all i get are the attributes without the values.
id,groupId,vehicleId,username
id,groupId,vehicleId,username
id,groupId,vehicleId,username
id,groupId,vehicleId,username
python-3.x
add a comment |
I have some json files that I need to convert to csv.
I have written a very simple script, but the results are unexpected. Instead of having all contents of the json file written to the csv file, I only get the top level name with each letter separated by a comma. Here's the json file contents:
"drivers": ["id": 91907, "groupId": 9039, "vehicleId": 212014918234488, "username": "abrauer",
"id": 134763, "groupId": 9039, "vehicleId": 212014918234742, "username": "abarbosa",
"id": 134764, "groupId": 9039, "vehicleId": 212014918234709, "username": "btoole",
"id": 134766, "groupId": 9039, "vehicleId": 212014918234773, "username": "bheinsohn"]
and the code:
import csv
import json
infile = open("driver.json","r")
outfile = open("driver.csv","w")
writer = csv.writer(outfile)
for row in json.loads(infile.read()):
writer.writerow(row)
The results are as follows:
d,r,i,v,e,r,s. Should I expect to remove {"drivers": from the file before trying to convert?
Update...
I tried removing "drivers": from the file, as well as the trailing '', and the results is just as strange. Now, all i get are the attributes without the values.
id,groupId,vehicleId,username
id,groupId,vehicleId,username
id,groupId,vehicleId,username
id,groupId,vehicleId,username
python-3.x
add a comment |
I have some json files that I need to convert to csv.
I have written a very simple script, but the results are unexpected. Instead of having all contents of the json file written to the csv file, I only get the top level name with each letter separated by a comma. Here's the json file contents:
"drivers": ["id": 91907, "groupId": 9039, "vehicleId": 212014918234488, "username": "abrauer",
"id": 134763, "groupId": 9039, "vehicleId": 212014918234742, "username": "abarbosa",
"id": 134764, "groupId": 9039, "vehicleId": 212014918234709, "username": "btoole",
"id": 134766, "groupId": 9039, "vehicleId": 212014918234773, "username": "bheinsohn"]
and the code:
import csv
import json
infile = open("driver.json","r")
outfile = open("driver.csv","w")
writer = csv.writer(outfile)
for row in json.loads(infile.read()):
writer.writerow(row)
The results are as follows:
d,r,i,v,e,r,s. Should I expect to remove {"drivers": from the file before trying to convert?
Update...
I tried removing "drivers": from the file, as well as the trailing '', and the results is just as strange. Now, all i get are the attributes without the values.
id,groupId,vehicleId,username
id,groupId,vehicleId,username
id,groupId,vehicleId,username
id,groupId,vehicleId,username
python-3.x
I have some json files that I need to convert to csv.
I have written a very simple script, but the results are unexpected. Instead of having all contents of the json file written to the csv file, I only get the top level name with each letter separated by a comma. Here's the json file contents:
"drivers": ["id": 91907, "groupId": 9039, "vehicleId": 212014918234488, "username": "abrauer",
"id": 134763, "groupId": 9039, "vehicleId": 212014918234742, "username": "abarbosa",
"id": 134764, "groupId": 9039, "vehicleId": 212014918234709, "username": "btoole",
"id": 134766, "groupId": 9039, "vehicleId": 212014918234773, "username": "bheinsohn"]
and the code:
import csv
import json
infile = open("driver.json","r")
outfile = open("driver.csv","w")
writer = csv.writer(outfile)
for row in json.loads(infile.read()):
writer.writerow(row)
The results are as follows:
d,r,i,v,e,r,s. Should I expect to remove {"drivers": from the file before trying to convert?
Update...
I tried removing "drivers": from the file, as well as the trailing '', and the results is just as strange. Now, all i get are the attributes without the values.
id,groupId,vehicleId,username
id,groupId,vehicleId,username
id,groupId,vehicleId,username
id,groupId,vehicleId,username
python-3.x
python-3.x
edited Mar 25 at 1:58
Gary Glasspool
asked Mar 25 at 1:39
Gary GlasspoolGary Glasspool
306
306
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Whats going on is that your for loop is iterating over the keys of the json dictionary so you're calling writer.writerow('drivers')
. Since strings are iterable in python, writer.writerow
is interpreting each character in drivers a column in the row.
The best way to handle this is instead of using a csv.writer
object, use a csv.DictWriter
object (Docs). Your code would then look something like:
with open('driver.csv','w') as csv_f:
drivers = json.loads(infile.read())['drivers']
fields = list(drivers[0].keys())
dict_writer = csv.DictWriter(csv_f, fieldnames=fields)
dict_writer.writeheader()
for driver in drivers:
dict_writer.writerow(driver)
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%2f55330300%2fwhy-does-my-script-not-print-entire-contents-of-json-file-to-csv%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
Whats going on is that your for loop is iterating over the keys of the json dictionary so you're calling writer.writerow('drivers')
. Since strings are iterable in python, writer.writerow
is interpreting each character in drivers a column in the row.
The best way to handle this is instead of using a csv.writer
object, use a csv.DictWriter
object (Docs). Your code would then look something like:
with open('driver.csv','w') as csv_f:
drivers = json.loads(infile.read())['drivers']
fields = list(drivers[0].keys())
dict_writer = csv.DictWriter(csv_f, fieldnames=fields)
dict_writer.writeheader()
for driver in drivers:
dict_writer.writerow(driver)
add a comment |
Whats going on is that your for loop is iterating over the keys of the json dictionary so you're calling writer.writerow('drivers')
. Since strings are iterable in python, writer.writerow
is interpreting each character in drivers a column in the row.
The best way to handle this is instead of using a csv.writer
object, use a csv.DictWriter
object (Docs). Your code would then look something like:
with open('driver.csv','w') as csv_f:
drivers = json.loads(infile.read())['drivers']
fields = list(drivers[0].keys())
dict_writer = csv.DictWriter(csv_f, fieldnames=fields)
dict_writer.writeheader()
for driver in drivers:
dict_writer.writerow(driver)
add a comment |
Whats going on is that your for loop is iterating over the keys of the json dictionary so you're calling writer.writerow('drivers')
. Since strings are iterable in python, writer.writerow
is interpreting each character in drivers a column in the row.
The best way to handle this is instead of using a csv.writer
object, use a csv.DictWriter
object (Docs). Your code would then look something like:
with open('driver.csv','w') as csv_f:
drivers = json.loads(infile.read())['drivers']
fields = list(drivers[0].keys())
dict_writer = csv.DictWriter(csv_f, fieldnames=fields)
dict_writer.writeheader()
for driver in drivers:
dict_writer.writerow(driver)
Whats going on is that your for loop is iterating over the keys of the json dictionary so you're calling writer.writerow('drivers')
. Since strings are iterable in python, writer.writerow
is interpreting each character in drivers a column in the row.
The best way to handle this is instead of using a csv.writer
object, use a csv.DictWriter
object (Docs). Your code would then look something like:
with open('driver.csv','w') as csv_f:
drivers = json.loads(infile.read())['drivers']
fields = list(drivers[0].keys())
dict_writer = csv.DictWriter(csv_f, fieldnames=fields)
dict_writer.writeheader()
for driver in drivers:
dict_writer.writerow(driver)
answered Mar 25 at 2:01
Nathan HammNathan Hamm
843
843
add a comment |
add a comment |
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%2f55330300%2fwhy-does-my-script-not-print-entire-contents-of-json-file-to-csv%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