如何只读 python 中的 1 个单词?
How to read ONLY 1 word in python?
我创建了一个空文本文件,并向其中保存了一些内容。这是我保存的:
Saish ddd TestUser ForTestUse
这几个字前有个space。无论如何,我想知道如何使用 python 只读取文本文件中的 1 个 WORD。这是我使用的代码:
#Uncommenting the line below the line does literally nothing.
import time
#import mmap, re
print("Loading Data...")
time.sleep(2)
with open("User_Data.txt") as f:
lines = f.read() ##Assume the sample file has 3 lines
first = lines.split(None, 1)[0]
print(first)
print("Type user number 1 - 4 for using different user.")
ans = input('Is the name above correct?(y/1 - 4) ')
if ans == 'y':
print("Ok! You will be called", first)
elif ans == '1':
print("You are already registered to", first)
elif ans == '2':
print('Switching to accounts...')
time.sleep(0.5)
with open("User_Data.txt") as f:
lines = f.read() ##Assume the sample file has 3 lines
second = lines.split(None, 2)[2]
print(second)
#Fix the passord issue! Very important as this is SECURITY!!!
当我运行代码时,我的输出是:
Loading Data...
Saish
Type user number 1 - 4 for using different user.
Is the name above correct?(y/1 - 4) 2
Switching to accounts...
TestUser ForTestUse
如您所见,它同时显示“TestUser”和“ForTestUse”,而我只希望它显示“TestUser”。
当您给 split()
一个限制时,从该限制到末尾的所有项目都会合并。所以如果你这样做
lines = 'Saish ddd TestUser ForTestUse'
split = lines.split(None, 2)
结果是
['Saish', 'ddd', 'TestUser ForTestUse']
如果你只想要第三个字,请不要限制split()
。
second = lines.split()[2]
不传递任何参数直接使用即可None
lines.split()[2]
我理解你传递 (None, 2) 因为如果索引 2 没有值,你想得到 None,
检查列表中索引是否可用的简单方法
Python 2
2 in zip(*enumerate(lines.split()))[0]
Python 3
2 in list(zip(*enumerate(lines.split())))[0]
我创建了一个空文本文件,并向其中保存了一些内容。这是我保存的:
Saish ddd TestUser ForTestUse
这几个字前有个space。无论如何,我想知道如何使用 python 只读取文本文件中的 1 个 WORD。这是我使用的代码:
#Uncommenting the line below the line does literally nothing.
import time
#import mmap, re
print("Loading Data...")
time.sleep(2)
with open("User_Data.txt") as f:
lines = f.read() ##Assume the sample file has 3 lines
first = lines.split(None, 1)[0]
print(first)
print("Type user number 1 - 4 for using different user.")
ans = input('Is the name above correct?(y/1 - 4) ')
if ans == 'y':
print("Ok! You will be called", first)
elif ans == '1':
print("You are already registered to", first)
elif ans == '2':
print('Switching to accounts...')
time.sleep(0.5)
with open("User_Data.txt") as f:
lines = f.read() ##Assume the sample file has 3 lines
second = lines.split(None, 2)[2]
print(second)
#Fix the passord issue! Very important as this is SECURITY!!!
当我运行代码时,我的输出是:
Loading Data...
Saish
Type user number 1 - 4 for using different user.
Is the name above correct?(y/1 - 4) 2
Switching to accounts...
TestUser ForTestUse
如您所见,它同时显示“TestUser”和“ForTestUse”,而我只希望它显示“TestUser”。
当您给 split()
一个限制时,从该限制到末尾的所有项目都会合并。所以如果你这样做
lines = 'Saish ddd TestUser ForTestUse'
split = lines.split(None, 2)
结果是
['Saish', 'ddd', 'TestUser ForTestUse']
如果你只想要第三个字,请不要限制split()
。
second = lines.split()[2]
不传递任何参数直接使用即可None
lines.split()[2]
我理解你传递 (None, 2) 因为如果索引 2 没有值,你想得到 None, 检查列表中索引是否可用的简单方法
Python 2
2 in zip(*enumerate(lines.split()))[0]
Python 3
2 in list(zip(*enumerate(lines.split())))[0]