Python 读取一行末尾的\n
Python reading \n on the end of a line
所以我正在从一个文本文件中读取内容来制作字典,但是一旦它在行尾添加 \n... 这是为什么?
蟒蛇
irTable = {}
with open("devices.txt") as file:
for line in file:
value = line.split(",")
label = str(value[0])
freq = int(value[1])
state = str(value[2])
irTable[label] = freq, state
print(irTable)
文本文件
lamp, 000000, False
tv, 000000, False
bedside, 000000, False
pc, 000000, False
bed tv, 000000, False
你所有的行都有换行;您需要先将其删除,然后再处理该行:
value = line.rstrip('\n').split(",")
Python 不会为您删除它。没有参数的 str.rstrip()
method used here will remove any number of \n
newline characters from the end of the line; there will never be more than one. You could also extend this to any whitespace, on both ends of the string, by using str.strip()
。
您已经从字符串开始,因此无需在此处使用 str()
调用。如果您的行是逗号分隔的,您可以只使用 csv
模块并让 it 处理行尾:
import csv
irTable = {}
with open("devices.txt", newline='') as file:
for label, freq, state in csv.reader(file, skipinitialspace=True):
irTable[label] = int(freq), state
演示:
>>> from io import StringIO
>>> import csv
>>> demofile = StringIO('''\
... lamp, 000000, False
... tv, 000000, False
... bedside, 000000, False
... pc, 000000, False
... bed tv, 000000, False
... ''')
>>> irTable = {}
>>> for label, freq, state in csv.reader(demofile, skipinitialspace=True):
... irTable[label] = int(freq), state
...
>>> irTable
{'lamp': (0, 'False'), 'tv': (0, 'False'), 'bedside': (0, 'False'), 'bed tv': (0, 'False'), 'pc': (0, 'False')}
在被 ","
拆分之前从行中删除 "\n"
例如
irTable = {}
with open("111.txt") as file:
for line in file:
value = line.strip().split(",")
irTable[value[0].strip()] = int(value[1]), value[2].strip()
print(irTable)
输出:
{'tv': (0, 'False'), 'pc': (0, 'False'), 'lamp': (0, 'False'), 'bedside': (0, 'False'), 'bed tv': (0, 'False')}
所以我正在从一个文本文件中读取内容来制作字典,但是一旦它在行尾添加 \n... 这是为什么?
蟒蛇
irTable = {}
with open("devices.txt") as file:
for line in file:
value = line.split(",")
label = str(value[0])
freq = int(value[1])
state = str(value[2])
irTable[label] = freq, state
print(irTable)
文本文件
lamp, 000000, False
tv, 000000, False
bedside, 000000, False
pc, 000000, False
bed tv, 000000, False
你所有的行都有换行;您需要先将其删除,然后再处理该行:
value = line.rstrip('\n').split(",")
Python 不会为您删除它。没有参数的 str.rstrip()
method used here will remove any number of \n
newline characters from the end of the line; there will never be more than one. You could also extend this to any whitespace, on both ends of the string, by using str.strip()
。
您已经从字符串开始,因此无需在此处使用 str()
调用。如果您的行是逗号分隔的,您可以只使用 csv
模块并让 it 处理行尾:
import csv
irTable = {}
with open("devices.txt", newline='') as file:
for label, freq, state in csv.reader(file, skipinitialspace=True):
irTable[label] = int(freq), state
演示:
>>> from io import StringIO
>>> import csv
>>> demofile = StringIO('''\
... lamp, 000000, False
... tv, 000000, False
... bedside, 000000, False
... pc, 000000, False
... bed tv, 000000, False
... ''')
>>> irTable = {}
>>> for label, freq, state in csv.reader(demofile, skipinitialspace=True):
... irTable[label] = int(freq), state
...
>>> irTable
{'lamp': (0, 'False'), 'tv': (0, 'False'), 'bedside': (0, 'False'), 'bed tv': (0, 'False'), 'pc': (0, 'False')}
在被 ","
拆分之前从行中删除 "\n"
例如
irTable = {}
with open("111.txt") as file:
for line in file:
value = line.strip().split(",")
irTable[value[0].strip()] = int(value[1]), value[2].strip()
print(irTable)
输出:
{'tv': (0, 'False'), 'pc': (0, 'False'), 'lamp': (0, 'False'), 'bedside': (0, 'False'), 'bed tv': (0, 'False')}