문제

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