我正在从.csv中读取某些值,并且由于某些原因,在第一个值的开头添加了空格。有人知道为什么会这样吗?
输入:
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)
输出:
请注意,input.csv在第一个值的开头没有空格。同样使用lstrip()似乎无法解决此问题。
答案 0 :(得分:1)
尝试
bitcoin
请注意:
Traceback (most recent call last):
File "C:/Users/SMA/PycharmProjects/HW2/keygen.py", line 3, in <module>
from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress
File "C:\Users\SMA\PycharmProjects\HW2\venv\lib\site-packages\bitcoin\wallet.py", line 29, in <module>
import bitcoin.core.key
File "C:\Users\SMA\PycharmProjects\HW2\venv\lib\site-packages\bitcoin\core\key.py", line 33, in <module>
_ssl = ctypes.cdll.LoadLibrary(ctypes.util.find_library('ssl') or 'libeay32')
File "C:\Users\SMA\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 434, in LoadLibrary
return self._dlltype(name)
File "C:\Users\SMA\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 356, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
skipinitialspace:如果设置为True,则分隔符之后的任何空白都将被忽略。
答案 1 :(得分:1)
它可能是零宽度不间断空格(U + FEFF)代码点,并用作UTF16和UTF32编码文件的字节顺序标记(BOM),以及UTF-8编码文件的签名。 open('input.csv',encoding='utf-8-sig')
不会将其删除。假设您的.CSV以UTF-8编码,使用utf16
会删除签名(如果存在)。其他选项是utf32
和print(ascii(host))
。他们需要BOM并将其删除,但很可能是UTF-8。
使用>>> host = '\ufeffBob'
>>> print(x)
Bob
>>> print(ascii(x))
'\ufeffBob'
来查看实际的字符:
{{1}}
答案 2 :(得分:0)
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