Python - readline() loop placing all lines into same value slot in dictionary










0















I'm trying to create a dictionary of city names that can be accessed using numbers 0-99. I have a txt document where I'm reading the city names from, and I'm using python's readline() in a loop to enter each city into the dictionary. For some reason, my loop creates a list in the first value slot of the dictionary and the list contains all the city names, whereas I want each city to correspond to a different number in the dictionary. Why isn't the loop going onto the next key of the dictionary and entering the city in the next value slot?



Here's my chunk of code:



i = 0
cities =
inFile = open('cities.txt','r')

while i < 100:
str = inFile.readline()
str = [line.rstrip('n') for line in inFile]
cities[i] = str
i += 1


Thanks in advance










share|improve this question

















  • 3





    What is the use of str = inFile.readline(), if you are not using str, but replacing that in the very next line?

    – Austin
    Nov 13 '18 at 14:23






  • 1





    str is a keyword in python.

    – Lijo Jose
    Nov 13 '18 at 14:25











  • The next line doesn't replace str but just strips off any unnecessary characters at the end

    – Amin
    Nov 13 '18 at 15:33















0















I'm trying to create a dictionary of city names that can be accessed using numbers 0-99. I have a txt document where I'm reading the city names from, and I'm using python's readline() in a loop to enter each city into the dictionary. For some reason, my loop creates a list in the first value slot of the dictionary and the list contains all the city names, whereas I want each city to correspond to a different number in the dictionary. Why isn't the loop going onto the next key of the dictionary and entering the city in the next value slot?



Here's my chunk of code:



i = 0
cities =
inFile = open('cities.txt','r')

while i < 100:
str = inFile.readline()
str = [line.rstrip('n') for line in inFile]
cities[i] = str
i += 1


Thanks in advance










share|improve this question

















  • 3





    What is the use of str = inFile.readline(), if you are not using str, but replacing that in the very next line?

    – Austin
    Nov 13 '18 at 14:23






  • 1





    str is a keyword in python.

    – Lijo Jose
    Nov 13 '18 at 14:25











  • The next line doesn't replace str but just strips off any unnecessary characters at the end

    – Amin
    Nov 13 '18 at 15:33













0












0








0








I'm trying to create a dictionary of city names that can be accessed using numbers 0-99. I have a txt document where I'm reading the city names from, and I'm using python's readline() in a loop to enter each city into the dictionary. For some reason, my loop creates a list in the first value slot of the dictionary and the list contains all the city names, whereas I want each city to correspond to a different number in the dictionary. Why isn't the loop going onto the next key of the dictionary and entering the city in the next value slot?



Here's my chunk of code:



i = 0
cities =
inFile = open('cities.txt','r')

while i < 100:
str = inFile.readline()
str = [line.rstrip('n') for line in inFile]
cities[i] = str
i += 1


Thanks in advance










share|improve this question














I'm trying to create a dictionary of city names that can be accessed using numbers 0-99. I have a txt document where I'm reading the city names from, and I'm using python's readline() in a loop to enter each city into the dictionary. For some reason, my loop creates a list in the first value slot of the dictionary and the list contains all the city names, whereas I want each city to correspond to a different number in the dictionary. Why isn't the loop going onto the next key of the dictionary and entering the city in the next value slot?



Here's my chunk of code:



i = 0
cities =
inFile = open('cities.txt','r')

while i < 100:
str = inFile.readline()
str = [line.rstrip('n') for line in inFile]
cities[i] = str
i += 1


Thanks in advance







python dictionary file-io readline






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 14:20









AminAmin

31




31







  • 3





    What is the use of str = inFile.readline(), if you are not using str, but replacing that in the very next line?

    – Austin
    Nov 13 '18 at 14:23






  • 1





    str is a keyword in python.

    – Lijo Jose
    Nov 13 '18 at 14:25











  • The next line doesn't replace str but just strips off any unnecessary characters at the end

    – Amin
    Nov 13 '18 at 15:33












  • 3





    What is the use of str = inFile.readline(), if you are not using str, but replacing that in the very next line?

    – Austin
    Nov 13 '18 at 14:23






  • 1





    str is a keyword in python.

    – Lijo Jose
    Nov 13 '18 at 14:25











  • The next line doesn't replace str but just strips off any unnecessary characters at the end

    – Amin
    Nov 13 '18 at 15:33







3




3





What is the use of str = inFile.readline(), if you are not using str, but replacing that in the very next line?

– Austin
Nov 13 '18 at 14:23





What is the use of str = inFile.readline(), if you are not using str, but replacing that in the very next line?

– Austin
Nov 13 '18 at 14:23




1




1





str is a keyword in python.

– Lijo Jose
Nov 13 '18 at 14:25





str is a keyword in python.

– Lijo Jose
Nov 13 '18 at 14:25













The next line doesn't replace str but just strips off any unnecessary characters at the end

– Amin
Nov 13 '18 at 15:33





The next line doesn't replace str but just strips off any unnecessary characters at the end

– Amin
Nov 13 '18 at 15:33












1 Answer
1






active

oldest

votes


















1














You can directly iterate on each file line in Python. And it seems what you really need is a list of cities, not a dictionary.



So what you need is probably something like:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]


If you really want a dictionary, then convert the list to a dictionary, using index as a key:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]

citydict = index: city for index, city in enumerate(cities)





share|improve this answer

























  • yes thank you, that worked! I did only need a list, but I at first couldn't find a way to add to it using the loop, so thanks for killing two birds with one stone

    – Amin
    Nov 13 '18 at 15:23











  • cities = line is not required.

    – Austin
    Nov 13 '18 at 16:06










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283088%2fpython-readline-loop-placing-all-lines-into-same-value-slot-in-dictionary%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can directly iterate on each file line in Python. And it seems what you really need is a list of cities, not a dictionary.



So what you need is probably something like:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]


If you really want a dictionary, then convert the list to a dictionary, using index as a key:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]

citydict = index: city for index, city in enumerate(cities)





share|improve this answer

























  • yes thank you, that worked! I did only need a list, but I at first couldn't find a way to add to it using the loop, so thanks for killing two birds with one stone

    – Amin
    Nov 13 '18 at 15:23











  • cities = line is not required.

    – Austin
    Nov 13 '18 at 16:06















1














You can directly iterate on each file line in Python. And it seems what you really need is a list of cities, not a dictionary.



So what you need is probably something like:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]


If you really want a dictionary, then convert the list to a dictionary, using index as a key:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]

citydict = index: city for index, city in enumerate(cities)





share|improve this answer

























  • yes thank you, that worked! I did only need a list, but I at first couldn't find a way to add to it using the loop, so thanks for killing two birds with one stone

    – Amin
    Nov 13 '18 at 15:23











  • cities = line is not required.

    – Austin
    Nov 13 '18 at 16:06













1












1








1







You can directly iterate on each file line in Python. And it seems what you really need is a list of cities, not a dictionary.



So what you need is probably something like:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]


If you really want a dictionary, then convert the list to a dictionary, using index as a key:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]

citydict = index: city for index, city in enumerate(cities)





share|improve this answer















You can directly iterate on each file line in Python. And it seems what you really need is a list of cities, not a dictionary.



So what you need is probably something like:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]


If you really want a dictionary, then convert the list to a dictionary, using index as a key:



cities = 
with open("cities.txt", "r") as infile:
cities = [city.rstrip('n') for city in infile]

citydict = index: city for index, city in enumerate(cities)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 16:13

























answered Nov 13 '18 at 15:07









SebSeb

263




263












  • yes thank you, that worked! I did only need a list, but I at first couldn't find a way to add to it using the loop, so thanks for killing two birds with one stone

    – Amin
    Nov 13 '18 at 15:23











  • cities = line is not required.

    – Austin
    Nov 13 '18 at 16:06

















  • yes thank you, that worked! I did only need a list, but I at first couldn't find a way to add to it using the loop, so thanks for killing two birds with one stone

    – Amin
    Nov 13 '18 at 15:23











  • cities = line is not required.

    – Austin
    Nov 13 '18 at 16:06
















yes thank you, that worked! I did only need a list, but I at first couldn't find a way to add to it using the loop, so thanks for killing two birds with one stone

– Amin
Nov 13 '18 at 15:23





yes thank you, that worked! I did only need a list, but I at first couldn't find a way to add to it using the loop, so thanks for killing two birds with one stone

– Amin
Nov 13 '18 at 15:23













cities = line is not required.

– Austin
Nov 13 '18 at 16:06





cities = line is not required.

– Austin
Nov 13 '18 at 16:06

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283088%2fpython-readline-loop-placing-all-lines-into-same-value-slot-in-dictionary%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto