开发者

Python reading 2 strings from the same line

开发者 https://www.devze.com 2023-02-14 11:56 出处:网络
how can Iread once at a time 2 strings from a txt file, that are written on the same 开发者_如何学Goline?

how can I read once at a time 2 strings from a txt file, that are written on the same 开发者_如何学Goline?

e.g. francesco 10


# out is your file
out.readline().split() # result is ['francesco', '10']

Assuming that your two strings are separated by whitespace. You can split based on any string (comma, colon, etc.)


Why not read just the line and split it up later? You'd have to read byte-by-byte and look for the space character, which is very inefficient. Better to read the entire line, and then split the resulting string on the space, giving you two strings.


'francesco 10'.split()

will give you ['francesco', '10'].


for line in fi:
    line.split()

Its ideal to just iterate over a file object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号