Python Forum
Working with files and dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with files and dictionaries
#1
So,
txt = open('testfile.txt','r')

d= {}

numLines = 0
#reading = txt.readlines()
read = txt.readline()
print(read)

for line in txt.readlines():
    numLines += 1
    #print(numLines)
    if numLines not in d:
        d[numLines] = [txt.readline()]
print(d)
It's giving me the following:
Output:
{1: [''], 2: [''], 3: ['']}
I actually want it to count the number of lines, and for example, in line number 1, I want the values of the dictionary to have the contents of the file split into a list.
Reply


Messages In This Thread
Working with files and dictionaries - by OmarSinno - Oct-30-2017, 08:46 AM
RE: Working with files and dictionaries - by wavic - Oct-30-2017, 11:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Question about working with dictionaries Ashcora 13 4,391 Dec-27-2022, 09:09 PM
Last Post: Ashcora
  Working with excel files arsouzaesilva 6 4,563 Sep-17-2021, 06:52 PM
Last Post: arsouzaesilva
Question Working with existing files Gilush 7 4,262 Feb-10-2021, 08:55 PM
Last Post: Gilush
  filecmp is not working for UTF-8 BOM encoded files sureshnagarajan 3 3,383 Feb-10-2021, 11:17 AM
Last Post: sureshnagarajan
  working with more excel files Krszt 1 3,018 Mar-13-2019, 11:41 AM
Last Post: perfringo
  Working with dictionaries and keys netrate 9 7,314 Jun-01-2017, 05:29 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020