为什么不停地从字符串到列表来回转换呢?

2024-10-01 07:44:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文本文件,包含所有学生的姓名和我的编程课程的其他信息,如下所示:

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

Tags: name文本linestudent学生atunistrip
3条回答

您可以使用regex('(\w+),\W+(\w+)')从每行中获取Lastname和Name。你知道吗

像这样:

import re
re.match('(\w+(?:-\w+)*),\W+(\w+(?:-\w+)*)', 'Lastname, Name, uniname@uni.edu, uniname@student.edu, Student/Teacher').groups()

here获得了帮助(对于连字符正则表达式)。你知道吗

如果您试图处理一个文件,其中每一行都是一个逗号分隔的值列表,那么^{}模块就是用来处理这个文件的。你知道吗

在您的更新版本中,看起来它们实际上是一个选项卡分隔的值列表…但这只是CSV的方言(称为TSV),模块也可以很好地处理:

peps = open('ppl.txt', 'r')
reader = csv.reader(peps, delimiter='\t')
for row in reader:
    # here, row is a list of column values

您还可以使用csv.writer将行以CSV格式写回。如果要将这些行写入终端,甚至可以使用csv.writer(sys.stdout)。你永远不必处理分裂和加入的问题,这一切都由你来处理。你知道吗


但是,第一列本身就是一个lastname, first,您还需要对它进行解析。为此,我将使用str.splitstr.partition(具体取决于如果Cher在您的类中,您希望获得什么行为)。我也不确定是要在', '上拆分,还是在,上拆分,然后去掉空格。两种方法都很简单。例如:

lastname, _, firstname = row[0].partition(',')
writer.writerow((firstname.strip(), lastname.strip()))

当我们这样做的时候,对文件使用with语句总是更好的,所以我们也这样做吧。你知道吗

But my intention is just making a new text file containing just the Name, Lastname in a nice column.

import csv
with open('ppl.txt') as infile, open('names.txt', 'w') as outfile:
    reader = csv.reader(infile, delimiter='\t')
    writer = csv.writer(outfile)
    for row in reader:
        lastname, _, firstname = row[0].partition(',')
        writer.writerow((firstname.strip(), lastname.strip()))

我不太清楚你的空间问题是什么。如果在某些情况下制表符后面有空格,并且希望忽略它们,那么应该查看csv模块中的^{}选项。例如:

reader = csv.reader(infile, skipinitialspaces=True)

但是如果在实际列的中间有制表符和空格,并且您想去掉它们,那么您可能需要使用str.replace或正则表达式。例如:

lastname, _, firstname = row[0].partition(',')
firstname = re.sub(r'\s', '', firstname)
lastname = re.sub(r'\s', '', lastname)
writer.writerow((firstname, lastname))

这里的其他答案肯定对你有用,但这里有一个更简单的方法来完成你的任务:

# we can open both the input and output files at the same time
with open('ppl.txt', 'r') as fi, open('output.txt', 'w') as fo:
    for line in fi:
        split_line = line.split()
        fo.write("{0}, {1}\n".format(split_line[1], split_line[0].strip(',')))
        # if using Python 3, remove the numbers from the curly brackets

如果您不喜欢幻数,可以添加itemgetter模块:

import operator
retriever = operator.itemgetter(1, 0)

with open('ppl.txt', 'r') as fi, open('output.txt', 'w') as fo:
    for line in fi:
        f_name, l_name = retriever(line.split())
        fo.write("{0}, {1}\n".format(f_name, l_name.strip(',')))

相关问题 更多 >