Der Inhalt der Datei lautet wie folgt:
Alex <span style="color: #800080">100000</span><span style="color: #000000"> Rain </span><span style="color: #800080">80000</span><span style="color: #000000"> Egon </span><span style="color: #800080">50000</span><span style="color: #000000"> Yuan </span><span style="color: #800080">30000</span><span style="color: #000000"> #此处有一个空行!</span>
Sehen Sie sich nun an, wie Sie es verarbeiten und in eine Liste umwandeln!
salary_info = open(<span style="color: #800000">"</span><span style="color: #800000">salaryinfo.txt</span><span style="color: #800000">"</span>, <span style="color: #800000">"</span><span style="color: #800000">r+</span><span style="color: #800000">"</span>, encoding=<span style="color: #800000">"</span><span style="color: #800000">UTF-8</span><span style="color: #800000">"</span><span style="color: #000000">) salary_info_list </span>=<span style="color: #000000"> [] </span><span style="color: #0000ff">for</span> line <span style="color: #0000ff">in</span><span style="color: #000000"> salary_info.readlines(): </span><span style="color: #0000ff">if</span> line == <span style="color: #800000">'</span><span style="color: #800000">\n</span><span style="color: #800000">'</span><span style="color: #000000">: pass </span><span style="color: #0000ff">else</span><span style="color: #000000">: salary_info_list.append(list(line.split())) # 读到的列表以追加的方式追加到salary_list值中。</span>
Die Ausgabeergebnisse lauten wie folgt:
[[<span style="color: #800000">'</span><span style="color: #800000">Alex</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">100000</span><span style="color: #800000">'</span>], [<span style="color: #800000">'</span><span style="color: #800000">Rain</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">80000</span><span style="color: #800000">'</span>], [<span style="color: #800000">'</span><span style="color: #800000">Egon</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">50000</span><span style="color: #800000">'</span>], [<span style="color: #800000">'</span><span style="color: #800000">Yuan</span><span style="color: #800000">'</span>, <span style="color: #800000">'</span><span style="color: #800000">30000</span><span style="color: #800000">'</span>]]
Das obige ist der detaillierte Inhalt vonDie Lösung für das Problem leerer Zeilen beim Konvertieren von Python-Dateizeichenfolgen in Listen!. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!