[英]How to remove BOM from UTF-8 CSV file imported into SQLITE
I'm trying to remove the BOM from the imported table off a csv file. 我正在尝试从csv文件中的导入表中删除BOM。 Would any one know where I could apply the "utf-8-sig" to my code to successfully do this.
有谁知道我可以在我的代码中应用“ utf-8-sig”来成功做到这一点。
I've tried changing the filename.encode("utf-8") to filename.encode("utf-8-sig") but it doesn't seem to change anything. 我尝试将filename.encode(“ utf-8”)更改为filename.encode(“ utf-8-sig”),但似乎没有任何改变。 I'm not sure I'm understanding how encoding something works honestly.
我不确定我是否了解诚实编码的工作原理。
''' class csvrd(object): '''类csvrd(object):
def csvFile(self):
self.readFile('Tuning_Acronyms.csv')
def readFile(self, filename):
conn = sqlite3.connect("Database.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Actions (Acronym TEXT, Detail TEXT, Tuple INTEGER PRIMARY KEY)")
filename.encode('utf-8-sig')
cur.execute("DELETE FROM Actions")
with open(filename) as f:
reader = csv.reader(f)
for field in reader:
cur.execute("INSERT INTO Actions VALUES (?,?,NULL);", field)
cur.execute(("Update Actions SET Tuple = Tuple - 1 WHERE Tuple > 0 "))
conn.commit()
conn.close()
''' '''
My results always end up with the first row starting with '' 我的结果总是以“”开头的第一行结束
Example 例
['NT', 'No Tuning'] ['NT','No Tuning']
I would like to remove '' and a possible explanation of the relations of BOMS and encoding. 我想删除“”以及关于BOMS和编码之间关系的可能解释。
Anything helps. 任何帮助。 :)
:)
I figured it out myself. 我自己弄清楚了。
I applied the encoding to the Open() statement. 我将编码应用于Open()语句。
class csvrd(object):
# To be combined into connect()
def csvFile(self):
self.readFile('Acronyms.csv')
def readFile(self, filename):
conn = sqlite3.connect("database.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Actions (Acronym TEXT, Detail TEXT, Tuple INTEGER PRIMARY KEY)")
print(filename)
cur.execute("DELETE FROM Actions")
with open(filename, encoding = 'utf-8-sig') as f:
reader = csv.reader(f)
for field in reader:
cur.execute("INSERT INTO Actions VALUES (?,?,NULL);", field)
cur.execute(("Update Actions SET Tuple = Tuple - 1 WHERE Tuple > 0 "))
conn.commit()
conn.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.