title: How to properly print out a CSV file in python
body: <p>I'm trying to scrape some data, write that data to a CSV file, then import that CSV file into R to do some analysis on it.  This is more of a personal project to learn python and R, and I'm having some trouble printing to the CSV file.  What I have now prints out in a completely chaotic fashion and R really can't use the data in the way I want it to.  Does anyone have any tips? Here is the code for the csv file: </p>
  
  <pre><code>with open('cdc_data.csv','w', newline='',) as f:
     i = 0
     fieldnames = ['Country', 'Infected', 'Deaths']
     thewriter = csv.DictWriter(f, fieldnames=fieldnames)
     print("Country /", end=' ')
     print("Infected /", end=' ')
     print("Deaths /", end=' ')
     print("Location:", end=' ')
     print(" ")
     thewriter.writeheader()
     for x in infectionData:
        while i &lt;= 83:
           print(infectionData[i], end=' ')
           thewriter.writerow({'Country': infectionData[i]})
           i += 1
           print(infectionData[i], end=' ')
           thewriter.writerow({'Infected': infectionData[i]})
           i += 1
           print(infectionData[i], end=' ')
           thewriter.writerow({'Deaths': infectionData[i]})
           i += 1
           print(infectionData[i])
          # thewriter.writerow({'Location': infectionData[i]})
           i += 1
  </code></pre>
  
  <p>I added what the current output looks now, I'm trying to get it so it looks more like this
  Countries / Infected / Deaths / Location
  China     /  1771    /  170   /  Asia
  etc</p>
  
  <p>Here is what print(infectedData) outputs:</p>
  
  <pre><code>['China', '7,711', '170', 'Asia', 'Thailand', '14', '0', 'Asia', 'Hong Kong', '11', '0', 'Asia', 'Taiwan', '8', '0', 'Asia', 'Macau', '7', '0', 'Asia', 'Malaysia', '7', '0', 'Asia', 'Singapore', '10', '0', 'Asia', 'Japan', '8'
  </code></pre>
  
  <p>and keeps going</p>
  
  <p><a href="https://i.stack.imgur.com/27iIH.png" rel="nofollow noreferrer">Image of CSV output</a></p>