如何从多行输出到单行。我们也可以使用发电机吗?

时间:2017-10-27 05:30:18

标签: python scripting

请查看以下输出: -

Software_Engineer: 
Networking
Software_Engineering
Computer_Graphics
Development
Design

Mechanical_Engineer:
Automata
(There is a new line character here)

我希望输出应该是: -

Software_Engineer: Networking Software_Engineering Computer_Graphics Development Design

Mechanical_Engineer: Automata

到目前为止,我在python中编写了以下代码: -

with open("split_module.txt") as f:
    all_lines = f.readlines()
    keys_col1 = []
    values_col2 = []
    for ids, values in enumerate(all_lines):
        if ":" in all_lines[ids] and all_lines[ids+1] != "\n":
            keys_col1.append(values.strip())
            for value in keys_col1:
                print(value)

我现在得到的输出为: -

Software_Engineer:
Software_Engineer:
Mechanical_Engineer:

在这里,为什么" Software_Engineer:"重复两次,我怎么能得到输出: -

Software_Engineer: Networking Software_Engineering Computer_Graphics Development Design

Mechanical_Engineer: Automata

请建议我是Python新手....谢谢!! !!

3 个答案:

答案 0 :(得分:1)

试试此代码

KRAParameter = (null != itemEmployees[Business.Enums.Employees.Designation.ToString()])
    ? Convert.ToString(kraParameterColl.FirstOrDefault(tempKRAParameter => 
        tempKRAParameter.Designation == Convert.ToString(
            itemEmployees[Business.Enums.Employees.Designation.ToString()]).Split('#')[1]))
    : string.Empty

outflie.txt的合作

fle=open("C:\Python27\projects\infile.txt")
fle2=open("C:\Python27\projects\outfile.txt",'w')
lst=fle.readlines()
for i in lst:
    i=i.strip()
    if i.endswith(':'):
        fle2.write("\n")
        fle2.write(i)
    else:
        fle2.write(i)
        fle2.write(" ")
fle.close()
fle2.close()

答案 1 :(得分:0)

快速而肮脏

Exception in thread "MainThread" java.lang.ExceptionInInitializerError
Caused by: Traceback (most recent call last):
  File "D:\TensorFlow\TflearnExample\ChatBot.py", line 9, in <module>
    import nltk
  File "D:\Python\Lib\site-packages\nltk\__init__.py", line 137, in <module>
    from nltk.stem import *
  File "D:\Python\Lib\site-packages\nltk\stem\__init__.py", line 29, in <module>
    from nltk.stem.snowball import SnowballStemmer
  File "D:\Python\Lib\site-packages\nltk\stem\snowball.py", line 26, in <module>
    from nltk.corpus import stopwords
  File "D:\Python\Lib\site-packages\nltk\corpus\__init__.py", line 66, in <module>
    from nltk.corpus.reader import *
  File "D:\Python\Lib\site-packages\nltk\corpus\reader\__init__.py", line 105, in <module>
    from nltk.corpus.reader.panlex_lite import *
  File "D:\Python\Lib\site-packages\nltk\corpus\reader\panlex_lite.py", line 15, in <module>
    import sqlite3
SyntaxError: Non-ASCII character in file 'D:\Python\Lib\sqlite3\__init__.py', but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

然后你可以

def f1(txt):
     buffer = ""
     for ee in txt.splitlines():
         if ee.endswith(':'):
             if buffer:
                 yield buffer.strip()
             buffer = ""
         buffer += ee + " "
     yield buffer.strip()

你会使用相同的想法来读取我写的文件中的数据,好像这就是字符串

答案 2 :(得分:0)

我得到了所需的输出: -

fle=open("C:\Python27\projects\infile.txt")
fle2=open("C:\Python27\projects\outfile.txt",'w')
lst=fle.readlines()
for i in lst:
    i=i.strip()
    if i.endswith(':'):
        fle2.write("\n")
        fle2.write(i)
    else:
        fle2.write(i)
        fle2.write(" ")
fle.close()
fle2.close()

感谢您的帮助..