我正在尝试在python中读取.txt文件,并将结果存储在列表中。我的txt文件看起来像这样:
Roll no 12
Name Abc
Date of birth 11/11/90
列表中的预期输出:
['Roll no','12','Name','Abc','Date of birth','11/11/90']
使用readLines()后得到的输出:
['Roll','no','12','Name','Abc','Date','of','birth','11/11/90']
答案 0 :(得分:1)
在没有看到实际代码的情况下,很难确切知道这里出了什么问题。
readlines
将在换行符分隔符上分割文件。为了获得完全的跨平台兼容性,请以“通用”兼容性模式(PEP-278 https://www.python.org/dev/peps/pep-0278/)打开文件,这避免了有关您的行以'\ n','\ r \ n'还是某些结尾的问题。其他变化形式(取决于您使用的是DOS还是类似Unix的系统):
with open('input.txt', 'rU') as t:
lines = t.readlines()
然后,您可以采用这组行,并假设您的输入使用制表符定界符,请将每行分成一个键/值对:
results = list()
for line in lines:
key,value = line.strip().split('\t')
results.append(key)
results.append(value)
有多种方法可以将其分解为列表理解,但是上面是一个非常明确的示例,说明了如何获得所需的结果。
答案 1 :(得分:0)
由于字段是制表符分隔的,因此您可以简单地按制表符分隔每一行。下面的示例假定文件以file
的形式打开:
[token for line in file for token in line.rstrip().split('\t')]
答案 2 :(得分:0)
另一种解决方案是读取行,然后将map()与strip()结合使用:
data4.txt:
<?php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class UpdatePassword
{
private $oldPassword;
/**
* @Assert\Length(min=8, minMessage="Le mot de passe doit être composé d'au moins 8 caractères")
*/
private $newPassword;
/**
* @Assert\EqualTo(propertyPath="newPassword", message="La confirmation du mot de passe ne correspond pas")
*/
private $confirmPassword;
public function getOldPassword(): ?string
{
return $this->oldPassword;
}
public function setOldPassword(string $oldPassword): self
{
$this->oldPassword = $oldPassword;
return $this;
}
public function getNewPassword(): ?string
{
return $this->newPassword;
}
public function setNewPassword(string $newPassword): self
{
$this->newPassword = $newPassword;
return $this;
}
public function getConfirmPassword(): ?string
{
return $this->confirmPassword;
}
public function setConfirmPassword(string $confirmPassword): self
{
$this->confirmPassword = $confirmPassword;
return $this;
}
}
代码:
security:
role_hierarchy:
ROLE_PRO: ROLE_USER
ROLE_ADMIN: [ROLE_USER, ROLE_PRO]
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
encoders:
App\Entity\User:
algorithm: auto
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
in_memory: { memory: ~ }
in_database:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
remember_me:
secret: '%kernel.secret%'
lifetime: 604800 # 1 week in seconds
path: /
provider: in_database
form_login:
login_path: account_login
check_path: account_login
default_target_path: account_index
logout:
path: account_logout
target: account_login
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/account, roles: ROLE_USER }
# - { path: ^/profile, roles: ROLE_ADMIN }
结果:
Roll no 12
Name Abc
Date of birth 11/11/90
要删除12,Abc和11/11/90:
fileName = 'data\data4.txt'
with open(fileName) as f:
lines= f.readlines()
lines= map(lambda s: s.strip(), lines)
list(lines)
结果:
['Roll no 12', 'Name Abc', 'Date of birth 11/11/90']