Sie können groupby
und count
von itertools
Modul zusammen mit list comprehension
verwenden.
Diese Antwort ist ein wenig hacky, aber mach den Trick. Sehen Sie sich die Kommentare an, um die dahinter stehende Logik besser zu verstehen.
Ich gehe davon aus, dass Ihr Eingang ist der Eingang, den Sie in einer Datei mit dem Namen gaben wird my_input_file
und Ausgabedatei output_file
genannt:
from itertools import groupby, count
# Read the file and split by the space between Value and its number
# Leave the case where the empty string '' exists without splitting its spaces
with open("my_input_file", 'r') as f:
data = (k.split() if k != '' else k for k in f.read().splitlines())
# Group the fields splitted, which are lists, in data
# And separate them by the field where the string 'Blank' exists
sub = [list(v) for _, v in groupby(data, lambda x: isinstance(x, list))]
final = []
for elm in sub:
# if the lenght of the grouped elements is > 1
if len(elm) >1:
# Convert the number of the values into an int
# For further calculations
dd = map(lambda x: [x[0], int(x[1])], elm)
# Group the consecutive numbers of elem
for _,v in groupby(dd , lambda x,y=count(): x[1] - next(y)):
# If there is a consecutive numbers
bb = list(v)
if len(bb) >1:
# Conveert them into strings. Then, append the first and the final one to the final list
final.append(' '.join(map(str, bb[0])) + ',' + ' '.join(map(str, bb[-1])))
# If there is'nt any consecutif numbers. Append the element to the final list
else:
final.append(" ".join(map(str, bb[0])))
# create the output file
with open("output_file", 'a') as f:
for k in final:
f.write(k + '\n')
Dieser Code wird ausgegeben eine Datei, die enthält:
Value 1,Value 3
Value 4,Value 5
Value 6,Value 8
Value 9,Value 11
Value 12,Value 13
Testen Sie diesen Code und lassen Sie Ihre Rückmeldungen, wenn Sie irgendwelche haben, oder vielleicht, melden Sie bitte Fehler, wenn Sie einen von ihnen gefunden haben.
Edit:
nach Ihren zuletzt.
Wenn Ihre Eingabedatei:
What Test
Makes No Sense
is This
My name
Is Sample 123
Your Name
is ABC 2134
What is you
technical question don't know
name?
Der Trick ist so einfach. Sie können etwas tun, nur mit groupby
von itertools
Modul:
from itertools import groupby
with open("my_input_file", 'r') as f:
data = f.read().splitlines()
final = [list(v) for _, v in groupby(data, lambda x: x != '')]
with open("ouput_file", 'a') as f:
for k in final:
if k != ['']:
f.write(k[0] + ',' + k[-1] + '\n')
Und wird Ihre Ausgabedatei:
What Test ,is This
My name ,Is Sample 123
Your Name ,is ABC 2134
What is you ,name?
Jeder Code, den Sie bisher geschrieben haben? (Bitte poste es; da SO kein Kodierungsdienst ist, ist deine Frage, wie geschrieben, off-topic.) – DyZ
Eine Zeile: ''\ n'.join ([' {}, {} '. Format (l [ 0], l [-1]) für l in [b.splitlines() für b in f_in.read(). Split ('\ n \ n')]]) ' – dawg