# Stripping whitespace in a CSV file

I am interested in removing leading/trailing whitespace in a .csv file, and I was wondering if there's a better way to execute this:

with open("csv_file.csv", "rb") as infile:
fieldnames = r.fieldnames #Creates list of fieldnames

for row in r:
for f in fieldnames:
row[f] = row[f].strip()


I am fine with using this method, but I was wondering if there's a way to circumvent using a nested for loop.

I don't think you need the b flag when opening a csv file. Python 3 doesn't let me use it. I think you can drop it.

To eliminate one level of nesting, you can use a dict comprehension, and the update method of a dictionary:

with open("/path/to/file.csv") as infile:

row.update({fieldname: value.strip() for (fieldname, value) in row.items()})


Notice that I also renamed the variables, as they were very poorly named. I also removed the "r" flag from the open, as that's the default mode anyway.

I don't think there's a simpler way to do this. I tried using a dialect, but this is the best I could get:

with open("/path/to/file.csv") as infile:
csv.register_dialect('strip', skipinitialspace=True)

Which does NOT work as intended: whitespace is stripped from the left of each field, but not from the right. My first thought was actually to set delimiter=r'\s*,\s*' when registering the dialect, but that doesn't work because delimiter must be a single character, it cannot be a regex. So, if you want to strip spaces from the values, you have no choice but do it yourself.