在x个字符后拆分字符串

时间:2017-02-10 17:03:44

标签: python python-2.7 python-3.x split

我正在尝试创建一个程序,通过文本文件从输入到函数的任何名称中创建一个3字母的字母组合图。这是代码!

output = 0
output_str = []
out = []

with open('Prob01.in.txt') as f:
    lines = f.read()
    for i in lines:
        if not i.isdigit():
            output_str.append(i)
    result = ''.join(output_str)
    result = result.split()
    for names in result:
        result = names[0].title()
        out.append(result)
    print ''.join(out)

这是文本文件!

2
2
Franklin Delano Roosevelt
gabriel de paula
3
samuel a jackson
robert j cuttle
fernanda d holland

我要找的结果是:

FDR
GDP
SAJ

然而,我得到了:

FDRGDPSAJRJCFDH

我认为在x个字符之后我需要.split(),在这种情况下为3,但是我将如何实现呢?

请记住,我对Python很新,所以如果有更好的方法,我会道歉。我很想知道是否有。

谢谢!

其他:

我还需要将结果输出到文件中,我试过:

output.write(''.join(monogram))

将输出设置为[],结果全部打印在一行中。

FDRGDPSAJRJCFDH

3 个答案:

答案 0 :(得分:0)

您需要先将String拆分为3个Char部分,然后再将它们发送到连接,并将连接更改为\ n,以便在每个人的姓名之间插入换行符:

output = 0
output_str = []
out = []

with open('Prob01.in.txt') as f:
    lines = f.read()
    for i in lines:
        if not i.isdigit():
            output_str.append(i)
    result = ''.join(output_str)
    result = result.split()
    for names in result:
        result = names[0].title()
        out.append(result)
    print '\n'.join(map(''.join, zip(*[iter(out)]*3)))

输出:

FDR
GDP
SAJ
RJC
FDH

虽然更好的方法可能是使用正则表达式:

import re
with open('Prob01.in.txt') as f:
    for m in re.findall(r"([a-zA-Z])\S* ([a-zA-Z])\S* ([a-zA-Z])\S*", f.read().upper()):
        print ''.join(m)

输出:

FDR
GDP
SAJ
RJC
FDH

答案 1 :(得分:0)

试试这个:

import re

with open('Prob01.in.txt', 'r') as f, open('output.txt', 'w+') as output:
    monograms = []

    lines = f.readlines()
    pattern = re.compile("^\D+\n")  # Any non-digit simbols in line
    name_list = filter(lambda line: re.match(pattern, line), lines)
    for name in name_list:
        monograms.append(''.join(map(lambda x: x[0].title(), name.split())))

    output.write('\n'.join(monograms))

with open('Prob01.in.txt', 'r') as f, open('output.txt', 'w+') as output:
    monograms = []

    for line in f.readlines():
        line = line.strip()
        if line and not line.isdigit():
            monogram = []
            for word in line.split():
                monogram.append(word[0].upper())
            monograms.append(''.join(monogram))

    output.write('\n'.join(monograms))

答案 2 :(得分:0)

您可以在list comprehension中获得所需的输出,如下例所示:

我假设您的输入文件名为input_file

data = (k.rstrip().split() for k in open("input_file", 'r'))
subfinal = ("".join(j[0].upper() for j in k if not j.isdigit()) for k in data)
final = [k for k in subfinal if k != '']
for k in final:
    print(k)

输出:

FDR
GDP
SAJ
RJC
FDH