Strip White Spaces from CSV File

How to read csv data, strip spaces/tabs and write to new csv file?

Don't make life complicated for yourself, "CSV" files are simple plain text files, and can be handled in a generic way:

with open('input.csv', 'r') as inf, open('output.csv', 'w') as of:
for line in inf:
trim = (field.strip() for field in line.split(','))
of.write(','.join(trim)+'\n')

Alternatively, using the csv module:

import csv

with open('input.csv', 'r') as inf, open('output.csv', 'w') as of:

r = csv.reader(inf, delimiter=',')
w = csv.writer(of, delimiter=',')

for line in r:
trim = (field.strip() for field in line)
w.writerow(trim)

remove space in csv file with python

I found the answer:

text = open("file.csv", "r", encoding="utf8")
text = ''.join([i for i in text]) \
.replace(" ", "")
x = open("file1" + i + ".csv", "w", encoding="utf8")
x.writelines(text)
x.close()

Remove leading and trailing white spaces in a csv file

You can use split(), strip() and join() like this:

','.join([item.strip() for item in my_string.split(',')])

Output:

>>> my_string = " a  , b,c, hello there !   ,   my name is +++ ,  g "
>>> ','.join([item.strip() for item in my_string.split(',')])
'a,b,c,hello there !,my name is +++,g'

Explanation:

split() is used to split my_string by the separator , and the result is the following list:

>>> my_string.split(',')
[' a ', ' b', 'c', ' hello there ! ', ' my name is +++ ', ' g ']

strip() is used to remove leading and trailing spaces from each item of the previous list:

>>> [item.strip() for item in my_string.split(',')]
['a', 'b', 'c', 'hello there !', 'my name is +++', 'g']

The above line is called a list comprehension

join() is used to form the last result by joining the items of the above list.

Trim leading whitespace from fields in csv file in Linux

sed 's/,[[:blank:]]*/,/g' file

Example:

> cat test.txt 
thing1,thing2, thing something3,thing4
thing12,thing12, thing something13,thing14

> cat test.txt | sed 's/,[[:blank:]]*/,/g'
thing1,thing2,thing something3,thing4
thing12,thing12,thing something13,thing14

R fread and strip white

There is a parameter strip.white which is set by default to TRUE in fread right now and you can also pass data.table = FALSE to fread to receive a data.frame after reading the dataset

Remove whitespace from CSV row

Your test.csv file contains narrow no-break space (U+202F) unicode characters. This is a non-whitespace character. (A regular space character is U+0020.)

You can see the different possible unicode spaces here: http://jkorpela.fi/chars/spaces.html

Here is a more generic script - using a POSIX bracket group - to remove all "space-like" characters:

require 'csv'
CSV.foreach('test.csv') do |row|
row = row[0]
row = row.gsub(/[[:space:]]/,"")
puts row
end

How can trailing spaces be removed from CSV import?

You will need to use strip() on each of the columns as follows:

import csv

with open('input.csv', 'rb') as f_input:
for row in csv.reader(f_input):
row = [col.strip() for col in row]

print row

This would print:

['1', 'A', 'a']
['2', 'B', 'b']
['99', 'ZZ', 'zz']

The csv library does provide an option called skipinitialspace=True but this will only deal with leading spaces.



Related Topics



Leave a reply



Submit