小编Cir*_*rdi的帖子

文件未正确读取

我正在尝试读取一个文件,然后将其存储到以年为关键字的字典中.但是,当我读取文件时,它一次跳过一堆行.该文件包含所有世界系列赛冠军(1903-2014).

fileName = input("Enter file name: ")
try:
    with open(fileName) as f:
        for data in f:
            data = f.readline().strip("\n")
            print(data) ##used to test to make sure the data is being read 
except:
    print("file doesnt exist")
Run Code Online (Sandbox Code Playgroud)

运行后的输出:

1905,New York Giants
1907,Chicago Cubs
1909,Pittsburgh Pirates
1911,Philadelphia Athletics
1913,Philadelphia Athletics
1915,Boston Red Sox
1917,Chicago White Sox
...
Run Code Online (Sandbox Code Playgroud)

文件

1903,Boston Americans
1905,New York Giants
1906,Chicago White Sox
1907,Chicago Cubs
1908,Chicago Cubs
1909,Pittsburgh Pirates
Run Code Online (Sandbox Code Playgroud)

python dictionary file readline

0
推荐指数
1
解决办法
47
查看次数

标签 统计

dictionary ×1

file ×1

python ×1

readline ×1