Python 3.x--file usage of seek, tell

Source: Internet
Author: User

When the file reads the content, it is possible to find open a file, and then read to the list or dict can only read once, and then define a variable to be assigned to the contents of the file will be removed null value.

With open ('./goods.txt ', ' R ') as FG:
result = List (Line.strip (). Split (': ') for line on FG if line)
RESULT2 = Dict (Line.strip (). Split (': ') for line on FG if line) #取出为空值

Cause: This is because when the file is read, the pointer moves from the beginning of the file to the end of the file, and then when the variable is defined, it starts reading from the end of the file.

Solution: Combine seek, tell method

With open ('./goods.txt ', ' R ') as FG:
result = List (Line.strip (). Split (': ') for line on FG if line)
Fg.seek (0)
RESULT2 = Dict (Line.strip (). Split (': ') for line on FG if line)

Tell method-the position where the character is removed

Python 3.x--file usage of seek, tell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.