Oamar Kanji
Oamar Kanji

Reputation: 2224

Unexplainable leading space only in first value when reading from csv

I am reading some values from a .csv and for some reason a space is being appended to the start of the first value. Does anyone know why this may be?

INPUT:

enter image description here

    with open('input.csv') as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in csv_reader:

        host = row[0]
        destination = row[1]
        port = row[2]
        print("HOST")
        print(host)

OUTPUT:

enter image description here

Note that the input.csv does not have any spaces at the beginning of the first value. Also using lstrip() does not seem to fix this.

Upvotes: 0

Views: 70

Answers (3)

Mark Tolonen
Mark Tolonen

Reputation: 177971

It's probably a ZERO WIDTH NO-BREAK SPACE (U+FEFF) code point and is used as a byte-order mark (BOM) for UTF16- and UTF32-encoded files and a signature for UTF-8-encoded files. It isn't removed by .strip(). Using open('input.csv',encoding='utf-8-sig') removes the signature if present, assuming your .CSV is encoded in UTF-8. Other options are utf16 and utf32. They require the BOM and remove it as well, but most likely it is UTF-8.

Use print(ascii(host)) to see what the character actually is:

>>> host = '\ufeffBob'
>>> print(x)
 Bob
>>> print(ascii(x))
'\ufeffBob'

Upvotes: 1

Karn Kumar
Karn Kumar

Reputation: 8826

Try with

with open('input.csv') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:

    host = row[0]
    host = host.strip() #  choo off whitespace
    destination = row[1]
    port = row[2]
    print("HOST")
    print(host)

side by Note:

csv_reader = csv.reader(csv_file, skipinitialspace=True)

skipinitialspace: If set to True, any white space immediately following the delimiter is ignored.

Upvotes: 1

TH Z
TH Z

Reputation: 1

 with open('input.csv') as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in csv_reader:

        host = row[0]
        destination = row[1]
        port = row[2]
        print("HOST")
        print(host) #  Remove spaces by treating strings
        print(host.strip()) # Default is a blank character

Upvotes: 0

Related Questions