简体   繁体   English

Python:读取csv并打印到控制台。 正在打印奇怪的字符

[英]Python: reading a csv and printing to console. strange characters are being printed

I am reading from a csv file and printing to console in Python. 我正在读取一个csv文件并打印到Python中的控制台。 The first printed line is including some odd characters at the beginning of the string. 第一行打印是在字符串的开头包含一些奇数字符。 The file in its entirety is: 整个文件为:

CSV范例

My code is: 我的代码是:

import csv

with open("C:\\Users\\user\\key.csv") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

the output is: 输出为:

['john', '12345']
['jacob', '23456']
['jingle', '34567']
['heimer', '45678']

I do not know where the "" in the first line is coming from. 我不知道第一行中的“”来自何处。

The "ï»" is a byte order mark (BOM) , which is used at the beginning of some Unicode files or streams to indicate the "endianness" of multi-byte encoding variants (UTF-16 or UTF-32), or, like in this case, to indicate that the file is using UTF-8. “ï»”是字节顺序标记(BOM) ,用于某些Unicode文件或流的开头,以指示多字节编码变体(UTF-16或UTF-32)的“字节序”,或者,例如在这种情况下,表示文件正在使用UTF-8。 Using a BOM at the beginning of a UTF-8 file is optional, but some applications -- like Microsoft Excel -- will use a BOM to indicate that the file is using UTF-8. 在UTF-8文件的开头使用BOM是可选的,但是某些应用程序(例如Microsoft Excel)将使用BOM来指示文件正在使用UTF-8。

If you're using Python 3 and you know that your file will be using UTF-8, you should be able to just add the encoding when you open the file: 如果您使用的是Python 3,并且知道文件将使用UTF-8,则打开文件时应该可以添加编码:

with open("C:\\Users\\user\\key.csv", encoding="utf-8-sig") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Here's the Python documentation on this: https://docs.python.org/3/howto/unicode.html#reading-and-writing-unicode-data 这是有关此内容的Python文档: https : //docs.python.org/3/howto/unicode.html#reading-and-writing-unicode-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM