Already found a solution for my original problem (by coding according to convention). However, I don't understand why I get two different csv files when I use open() in two different ways.
Using Pandas on Python 3.7, I normalized a Json list using pandas.io.json.json_normalize. The new DataFrame object has the desired dimensions (25 by 28).
>>>normalFrame = pd.io.json.json_normalize(jsonList)
>>> normalFrame.shape
(25, 28) #Awesome. So far, so good.
>>>filename = 'pandaNormJson.csv'
Now, the following attempts created two different files:
Attempt 1 (fail):
>>>newCsv = open(filename, 'w', newline='')
>>>normalFrame.to_csv(newCsv)
This creates a csv file with 18 out of 25 records.
Attempt 2 -proper way (success)
>>> with open(filename, 'w', newline='') as newCsv:
... normalFrame.to_csv(newCsv)
Now I have the desired 25x28 (without headers) csv file.
- Whats wrong with attempt 1?
- Whats the difference between
with open() as writer
andwriter = open()
? - Is it something behind the scenes of open() or pd.to_csv()?