我有一个文本文件,包含所有学生的姓名和我的编程课程的其他信息,如下所示:
Smith, John sj0012@uni.edu smjo0012@student.edu Student
Lester, Moe mole0025@uni.edu mole0025@student.edu Student
Christ, Jesus jech0020@uni.edu jech@student.edu Student
。。。你知道吗
其中一些在每行的文本之间包含制表符和其他不必要的空格。因此,第一个电子邮件地址和第二个是标签。有时在这两者之间和“学生”之间。但我的目的只是制作一个新的文本文件,其中只包含名字,在一个不错的列中的姓氏。 我确实得到了我的结果,但只是不断地将文本转换成列表,再转换回字符串。有没有更好的办法? Python 2.7版
peps = open('ppl.txt', 'r')
for line in peps.readlines():
line = line.strip() # Delete space
line = line.split('\t') # Split at tab indentation and make a list
line = map(lambda s: s.strip(), line) # Remove tab indentation
del line [1:] # Delete everything after Name.
line = ','.join(line) # Make Lastname, Name a string at ','
line = line.split(',') # Make Lastname, Name a list at ','
line[0], line[-1] = line[-1], line[0] # Exchange position of Lastname, Name
line = ', '.join(line) # Convert to string again and join at ','
print line
您可以使用regex(
'(\w+),\W+(\w+)'
)从每行中获取Lastname和Name。你知道吗像这样:
从here获得了帮助(对于连字符正则表达式)。你知道吗
如果您试图处理一个文件,其中每一行都是一个逗号分隔的值列表,那么^{} 模块就是用来处理这个文件的。你知道吗
在您的更新版本中,看起来它们实际上是一个选项卡分隔的值列表…但这只是CSV的方言(称为TSV),模块也可以很好地处理:
您还可以使用
csv.writer
将行以CSV格式写回。如果要将这些行写入终端,甚至可以使用csv.writer(sys.stdout)
。你永远不必处理分裂和加入的问题,这一切都由你来处理。你知道吗但是,第一列本身就是一个
lastname, first
,您还需要对它进行解析。为此,我将使用str.split
或str.partition
(具体取决于如果Cher在您的类中,您希望获得什么行为)。我也不确定是要在', '
上拆分,还是在,
上拆分,然后去掉空格。两种方法都很简单。例如:当我们这样做的时候,对文件使用
with
语句总是更好的,所以我们也这样做吧。你知道吗我不太清楚你的空间问题是什么。如果在某些情况下制表符后面有空格,并且希望忽略它们,那么应该查看} 选项。例如:
csv
模块中的^{但是如果在实际列的中间有制表符和空格,并且您想去掉它们,那么您可能需要使用
str.replace
或正则表达式。例如:这里的其他答案肯定对你有用,但这里有一个更简单的方法来完成你的任务:
如果您不喜欢幻数,可以添加
itemgetter
模块:相关问题 更多 >
编程相关推荐