Fix Python – How to convert CSV file to multiline JSON?

Here’s my code, really simple stuff…
import csv
import json

csvfile = open(‘file.csv’, ‘r’)
jsonfile = open(‘file.json’, ‘w’)

fieldnames = (“FirstName”,”LastName”,”IDNumber”,”Message”)
reader = csv.DictReader( csvfile, fieldnames)
out = json.dumps( [ row for row in reader ] )
jsonfile.write(out)

Declare some field names, the reader uses CSV t….

Fix Python – pandas read_csv and filter columns with usecols

I have a csv file which isn’t coming in correctly with pandas.read_csv when I filter the columns with usecols and use multiple indexes.

import pandas as pd
csv = r”””dummy,date,loc,x
bar,20090101,a,1
bar,20090102,a,3
bar,20090103,a,5
bar,20090101,b,1
bar,20090102,b,3
bar,20090103,b,5″””

f = open(‘foo.csv’, ‘w’)
f.write(csv)
f…..

Fix Python – Why does csvwriter.writerow() put a comma after each character?

This code opens the URL and appends the /names at the end and opens the page and prints the string to test1.csv:
import urllib2
import re
import csv

url = (“http://www.example.com”)
bios = [u’/name1′, u’/name2′, u’/name3′]
csvwriter = csv.writer(open(“/test1.csv”, “a”))

for l in bios:
OpenThisLink = url + l
response = urllib2.urlopen(Ope….

Fix Python – Import CSV file as a Pandas DataFrame

How do I read the following CSV file into a Pandas DataFrame?
Date,”price”,”factor_1″,”factor_2″
2012-06-11,1600.20,1.255,1.548
2012-06-12,1610.02,1.258,1.554
2012-06-13,1618.07,1.249,1.552
2012-06-14,1624.40,1.253,1.556
2012-06-15,1626.15,1.258,1.552
2012-06-16,1626.15,1.263,1.558
2012-06-17,1626.15,1.264,1.572

….

Fix Python – Load CSV file with PySpark

I’m new to Spark and I’m trying to read CSV data from a file with Spark.
Here’s what I am doing :
sc.textFile(‘file.csv’)
.map(lambda line: (line.split(‘,’)[0], line.split(‘,’)[1]))
.collect()

I would expect this call to give me a list of the two first columns of my file but I’m getting this error :

File “”, line 1, in
IndexError: list ….

Fix Python – Way to read first few lines for pandas dataframe

Is there a built-in way to use read_csv to read only the first n lines of a file without knowing the length of the lines ahead of time? I have a large file that takes a long time to read, and occasionally only want to use the first, say, 20 lines to get a sample of it (and prefer not to load the full thing and take the head of it).
If I knew the t….

Fix Python – Skip rows during csv import pandas

I’m trying to import a .csv file using pandas.read_csv(), however, I don’t want to import the 2nd row of the data file (the row with index = 1 for 0-indexing).
I can’t see how not to import it because the arguments used with the command seem ambiguous:
From the pandas website:

skiprows : list-like or integer
Row numbers to skip (0-indexed) or num….