The solution to the problem of empty lines when converting Python file strings to lists!

PHP中文网
Release: 2017-07-08 18:13:18
Original
1830 people have browsed it

The file content is as follows:

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>
Copy after login

Now see how to process and convert it into a list!

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>
Copy after login

The output results are as follows:

[[<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>]]
Copy after login

The above is the detailed content of The solution to the problem of empty lines when converting Python file strings to lists!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template