I have an excel file which I saved as csv. When I open and print the csv using python, I get some control characters at the start of the row. The control character is \x01. Some rows have one of them in front, other have more than one. Basically in the excel file, the author indented the start of line text in some of the rows.

So far so good. I want to write a small if statement checking if there is a control character and then do something

import csv
f = open('/Users/Downloads/Book1.csv', 'r')
csv_f = csv.reader(f)

for row in csv_f:
    if row[1][0] is \x01:
        print("hello")

Can someone help me how to compare the control character?

有帮助吗?

解决方案

Just put quotes around your escape sequence. And don't use is in this case.

if column[1][0] == '\x01':
    print('hello')

Also, a csv.reader emits a sequence of rows, not columns.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top