Я очень новичок в Python. Я хочу объединить строки, пока строка не будет найдена. Как только строка найдена, она должна быть новой строкой, а затем объединить остальные строки в абзаце.

Я попытался соединить строки, добавив разделитель, и это работает

fileindex = open('index1.txt')
print ";".join(line.strip() for line in fileindex)

Затем я попробовал итерацию, но он дал мне только строки, соответствующие последней строке:

with open('index1.txt', 'r') as content_file:
  indifile = content_file.read()
  for item in indifile.split("\n"):
      if "Group" in item:
        a = item.strip()
      if "Project" in item:
        b = item.strip()
      if "Manifest" in item:
        c = item.strip()
      if "POM" in item:
        d = item.strip()
      if "Embedded" in item:
        e = item.strip()
        indistrings = [a, b, c, d, e]
        sep = ';'
        print(sep.join(indistrings))

Файл выглядит так:

Group: ch.qos.logback Name: logback-core Version: 1.1.11 
Manifest Project URL: http://www.qos.ch
Manifest license URL: http://www.eclipse.org/legal/epl-v10.html,
http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
POM License: Eclipse Public License - v 1.0 http://www.eclipse.org/legal/epl-v10.html
POM License: GNU Lesser General Public License \- http://www.gnu.org/licenses
/old-licenses/lgpl-2.1.html

Group: com.aol.simplereact Name: cyclops-react Version: 2.0.0-FINAL 
POM Project URL: https://github.com/aol/simple-react
POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt

Group: com.fasterxml Name: classmate Version: 1.3.4 
Project URL: http://github.com/FasterXML/java-classmate
Manifest license URL: http://www.apache.org/licenses/LICENSE-2.0.txt
POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt
Embedded license files: [classmate-1.3.4.jar/METAINF/LICENSE](classmate-1.3.4.jar/META-INF/LICENSE)

Результат, который я хотел бы, должен быть таким:

Group: ch.qos.logback Name: logback-core Version: 1.1.11;Manifest Project URL: http://www.qos.ch;Manifest license URL: Manifest license URL: http://www.eclipse.org/legal/epl-v10.html, http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html;POM License: Eclipse Public License - v 1.0 http://www.eclipse.org/legal/epl-v10.html;POM License: GNU Lesser General Public License \- http://www.gnu.org/licenses
/old-licenses/lgpl-2.1.html

Group: com.aol.simplereact Name: cyclops-react Version: 2.0.0-FINAL;POM Project URL: https://github.com/aol/simple-react;POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt

and so on

Любая помощь приветствуется

1
spiceitup 4 Июл 2019 в 13:24

4 ответа

Лучший ответ

Итеративный подход для Python 3.x :

(с начальным разделителем ;)

with open('input.txt') as f:
    start_line = False          # flag indicating a starting line of a section
    for i, l in enumerate(f):   # iterate with counters (starting from `0`) 
        if not l.strip():       # on encountering empty line
            print(end='\n\n')
            start_line = True   # prepare for next new section
        else:
            print(('' if i == 0 or start_line else ';') + l.strip(), end='')
            start_line = False

Выход:

Group: ch.qos.logback Name: logback-core Version: 1.1.11;Manifest Project URL: http://www.qos.ch;Manifest license URL: http://www.eclipse.org/legal/epl-v10.html,;http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html;POM License: Eclipse Public License - v 1.0 http://www.eclipse.org/legal/epl-v10.html;POM License: GNU Lesser General Public License \- http://www.gnu.org/licenses;/old-licenses/lgpl-2.1.html

Group: com.aol.simplereact Name: cyclops-react Version: 2.0.0-FINAL;POM Project URL: https://github.com/aol/simple-react;POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt

Group: com.fasterxml Name: classmate Version: 1.3.4;Project URL: http://github.com/FasterXML/java-classmate;Manifest license URL: http://www.apache.org/licenses/LICENSE-2.0.txt;POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt;Embedded license files: [classmate-1.3.4.jar/METAINF/LICENSE](classmate-1.3.4.jar/META-INF/LICENSE)
1
RomanPerekhrest 4 Июл 2019 в 11:58

Это не красиво, но вы можете сделать это

with open('demo_file.txt', 'r') as f:
    text = ''.join([i.replace('\n', ';') if i.strip() else '\n\n' for i in f.readlines()])

Результат выглядит как

Group: ch.qos.logback Name: logback-core Version: 1.1.11 ;Manifest Project URL: http://www.qos.ch;Manifest license URL: http://www.eclipse.org/legal/epl- v10.html,;http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html;POM License: Eclipse Public License - v 1.0 http://www.eclipse.org/legal/epl-v10.html;POM License: GNU Lesser General Public License \- http://www.gnu.org/licenses;/old-licenses/lgpl- 2.1.html;

Group: com.aol.simplereact Name: cyclops-react Version: 2.0.0-FINAL ;POM Project URL: https://github.com/aol/simple-react;POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt;

Group: com.fasterxml Name: classmate Version: 1.3.4 ;Project URL: http://github.com/FasterXML/java-classmate;Manifest license URL: http://www.apache.org/licenses/LICENSE-2.0.txt;POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt;Embedded license files: [classmate-1.3.4.jar/METAINF/LICENSE](classmate-1.3.4.jar/META-INF/LICENSE);
0
blackcircular 4 Июл 2019 в 11:02

Вы можете просто напечатать все строки с помощью '; 'вместо "\ n" и только если вы видите строку "Group" в строке, выведите два символа возврата.

mystring = """Group: ch.qos.logback Name: logback-core Version: 1.1.11
Manifest Project URL: http://www.qos.ch
Manifest license URL: http://www.eclipse.org/legal/epl-v10.html,
http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
POM License: Eclipse Public License - v 1.0 http://www.eclipse.org/legal/epl-v10.html
POM License: GNU Lesser General Public License \- http://www.gnu.org/licenses
/old-licenses/lgpl-2.1.html

Group: com.aol.simplereact Name: cyclops-react Version: 2.0.0-FINAL
POM Project URL: https://github.com/aol/simple-react
POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt

Group: com.fasterxml Name: classmate Version: 1.3.4
Project URL: http://github.com/FasterXML/java-classmate
Manifest license URL: http://www.apache.org/licenses/LICENSE-2.0.txt
POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt
Embedded license files: [classmate-1.3.4.jar/METAINF/LICENSE](classmate-1.3.4.jar/META-INF/LICENSE)
"""

for line in mystring.split("\n"):
    if "Group" in line:
        print("\n")
    if line.strip(" "):
        print(line.strip(), end='; ')

Это производит вывод

Group: ch.qos.logback Name: logback-core Version: 1.1.11; Manifest Project URL: http://www.qos.ch; Manifest license URL: http://www.eclipse.org/legal/epl-v10.html,; http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html; POM License: Eclipse Public License - v 1.0 http://www.eclipse.org/legal/epl-v10.html; POM License: GNU Lesser General Public License \- http://www.gnu.org/licenses; /old-licenses/lgpl-2.1.html; 

Group: com.aol.simplereact Name: cyclops-react Version: 2.0.0-FINAL; POM Project URL: https://github.com/aol/simple-react; POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt; 

Group: com.fasterxml Name: classmate Version: 1.3.4; Project URL: http://github.com/FasterXML/java-classmate; Manifest license URL: http://www.apache.org/licenses/LICENSE-2.0.txt; POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt; Embedded license files: [classmate-1.3.4.jar/METAINF/LICENSE](classmate-1.3.4.jar/META-INF/LICENSE); 
0
Chris Doyle 4 Июл 2019 в 10:42

Используя простую итерацию.

< Сильный > Пример:

data = """Group: ch.qos.logback Name: logback-core Version: 1.1.11 
Manifest Project URL: http://www.qos.ch
Manifest license URL: http://www.eclipse.org/legal/epl-v10.html,
http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
POM License: Eclipse Public License - v 1.0 http://www.eclipse.org/legal/epl-v10.html
POM License: GNU Lesser General Public License \- http://www.gnu.org/licenses
/old-licenses/lgpl-2.1.html

Group: com.aol.simplereact Name: cyclops-react Version: 2.0.0-FINAL 
POM Project URL: https://github.com/aol/simple-react
POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt

Group: com.fasterxml Name: classmate Version: 1.3.4 
Project URL: http://github.com/FasterXML/java-classmate
Manifest license URL: http://www.apache.org/licenses/LICENSE-2.0.txt
POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt
Embedded license files: [classmate-1.3.4.jar/METAINF/LICENSE](classmate-1.3.4.jar/META-INF/LICENSE)
"""

result = []
for line in data.splitlines():               #Iterate each line
    if not result or not line.strip():       #Check if result is empty or line is empty
        result.append([line.strip() + ";"])        #append empty list
    else:
        result[-1].append(line.strip() + ";")      #append line to previous line

result = ["".join(i).strip().strip(";") for i in result]        #Group lines together. 
print(result)

< Сильный > Вывод :

['Group: ch.qos.logback Name: logback-core Version: 1.1.11;Manifest Project URL: http://www.qos.ch;Manifest license URL: http://www.eclipse.org/legal/epl-v10.html,;http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html;POM License: Eclipse Public License - v 1.0 http://www.eclipse.org/legal/epl-v10.html;POM License: GNU Lesser General Public License \\- http://www.gnu.org/licenses;/old-licenses/lgpl-2.1.html',
 'Group: com.aol.simplereact Name: cyclops-react Version: 2.0.0-FINAL;POM Project URL: https://github.com/aol/simple-react;POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt',
 'Group: com.fasterxml Name: classmate Version: 1.3.4;Project URL: http://github.com/FasterXML/java-classmate;Manifest license URL: http://www.apache.org/licenses/LICENSE-2.0.txt;POM License: The Apache Software License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt;Embedded license files: [classmate-1.3.4.jar/METAINF/LICENSE](classmate-1.3.4.jar/META-INF/LICENSE)']
0
Rakesh 4 Июл 2019 в 12:51