Assigning variables to parts of a text file










1














I have a line from a text file that goes as follows:



[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC


I am trying to assign different parts of this line to specific variables with this code:



latitude = 0
longitude = 0
unused1 = 0
unused2 = 0
unused3 = 0
tweetWordList =
for line in tweetFile:
line = line.rstrip()
longitude,latitude,unused1,unused2,unused3,tweetWordList = line.split()


I am trying to get the chunk of text from the tweet into the tweetWordList but I get an error saying that there are too many values to unpack. How do I divide this line so that the writing goes into the list I created?



I already read in the file and the rest of the program to this point works fine.










share|improve this question























  • Read the docs about module re (regular expressions), play around with them a bit and you should find a way.
    – Michael Butscher
    Nov 12 '18 at 22:25
















1














I have a line from a text file that goes as follows:



[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC


I am trying to assign different parts of this line to specific variables with this code:



latitude = 0
longitude = 0
unused1 = 0
unused2 = 0
unused3 = 0
tweetWordList =
for line in tweetFile:
line = line.rstrip()
longitude,latitude,unused1,unused2,unused3,tweetWordList = line.split()


I am trying to get the chunk of text from the tweet into the tweetWordList but I get an error saying that there are too many values to unpack. How do I divide this line so that the writing goes into the list I created?



I already read in the file and the rest of the program to this point works fine.










share|improve this question























  • Read the docs about module re (regular expressions), play around with them a bit and you should find a way.
    – Michael Butscher
    Nov 12 '18 at 22:25














1












1








1







I have a line from a text file that goes as follows:



[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC


I am trying to assign different parts of this line to specific variables with this code:



latitude = 0
longitude = 0
unused1 = 0
unused2 = 0
unused3 = 0
tweetWordList =
for line in tweetFile:
line = line.rstrip()
longitude,latitude,unused1,unused2,unused3,tweetWordList = line.split()


I am trying to get the chunk of text from the tweet into the tweetWordList but I get an error saying that there are too many values to unpack. How do I divide this line so that the writing goes into the list I created?



I already read in the file and the rest of the program to this point works fine.










share|improve this question















I have a line from a text file that goes as follows:



[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC


I am trying to assign different parts of this line to specific variables with this code:



latitude = 0
longitude = 0
unused1 = 0
unused2 = 0
unused3 = 0
tweetWordList =
for line in tweetFile:
line = line.rstrip()
longitude,latitude,unused1,unused2,unused3,tweetWordList = line.split()


I am trying to get the chunk of text from the tweet into the tweetWordList but I get an error saying that there are too many values to unpack. How do I divide this line so that the writing goes into the list I created?



I already read in the file and the rest of the program to this point works fine.







python parsing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:31









mkrieger1

4,30121832




4,30121832










asked Nov 12 '18 at 22:21









Marco Aiello

454




454











  • Read the docs about module re (regular expressions), play around with them a bit and you should find a way.
    – Michael Butscher
    Nov 12 '18 at 22:25

















  • Read the docs about module re (regular expressions), play around with them a bit and you should find a way.
    – Michael Butscher
    Nov 12 '18 at 22:25
















Read the docs about module re (regular expressions), play around with them a bit and you should find a way.
– Michael Butscher
Nov 12 '18 at 22:25





Read the docs about module re (regular expressions), play around with them a bit and you should find a way.
– Michael Butscher
Nov 12 '18 at 22:25













1 Answer
1






active

oldest

votes


















1














It's because you're splitting on spaces, so all of the text gets split into list items as well. If the formatting is consistent, I'd suggest splitting on list indices:



>>> line = "[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC"
>>> splitline = line.split()
>>> longitude = splitline[0].replace('[', '').replace(',', '')
>>> latitude = splitline[1].replace(']', '')
>>> tweetWordList = ' '.join(splitline[5:])


Alternatively, you could do it with a regex pattern:



>>> import re
>>> latitude, longitude, tweetWordList = re.findall("^[([d.]+), ([d-.]+)] [d] [d]4-[d]2-[d]2 [d]2:[d]2:[d]2 ([A-Za-z0-9 .']+)", line)[0]


You'll need to play around with the regex pattern to match your text properly, but that's the gist of it.






share|improve this answer






















  • Thank you for the help
    – Marco Aiello
    Nov 12 '18 at 22:46










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%2f53270944%2fassigning-variables-to-parts-of-a-text-file%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














It's because you're splitting on spaces, so all of the text gets split into list items as well. If the formatting is consistent, I'd suggest splitting on list indices:



>>> line = "[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC"
>>> splitline = line.split()
>>> longitude = splitline[0].replace('[', '').replace(',', '')
>>> latitude = splitline[1].replace(']', '')
>>> tweetWordList = ' '.join(splitline[5:])


Alternatively, you could do it with a regex pattern:



>>> import re
>>> latitude, longitude, tweetWordList = re.findall("^[([d.]+), ([d-.]+)] [d] [d]4-[d]2-[d]2 [d]2:[d]2:[d]2 ([A-Za-z0-9 .']+)", line)[0]


You'll need to play around with the regex pattern to match your text properly, but that's the gist of it.






share|improve this answer






















  • Thank you for the help
    – Marco Aiello
    Nov 12 '18 at 22:46















1














It's because you're splitting on spaces, so all of the text gets split into list items as well. If the formatting is consistent, I'd suggest splitting on list indices:



>>> line = "[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC"
>>> splitline = line.split()
>>> longitude = splitline[0].replace('[', '').replace(',', '')
>>> latitude = splitline[1].replace(']', '')
>>> tweetWordList = ' '.join(splitline[5:])


Alternatively, you could do it with a regex pattern:



>>> import re
>>> latitude, longitude, tweetWordList = re.findall("^[([d.]+), ([d-.]+)] [d] [d]4-[d]2-[d]2 [d]2:[d]2:[d]2 ([A-Za-z0-9 .']+)", line)[0]


You'll need to play around with the regex pattern to match your text properly, but that's the gist of it.






share|improve this answer






















  • Thank you for the help
    – Marco Aiello
    Nov 12 '18 at 22:46













1












1








1






It's because you're splitting on spaces, so all of the text gets split into list items as well. If the formatting is consistent, I'd suggest splitting on list indices:



>>> line = "[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC"
>>> splitline = line.split()
>>> longitude = splitline[0].replace('[', '').replace(',', '')
>>> latitude = splitline[1].replace(']', '')
>>> tweetWordList = ' '.join(splitline[5:])


Alternatively, you could do it with a regex pattern:



>>> import re
>>> latitude, longitude, tweetWordList = re.findall("^[([d.]+), ([d-.]+)] [d] [d]4-[d]2-[d]2 [d]2:[d]2:[d]2 ([A-Za-z0-9 .']+)", line)[0]


You'll need to play around with the regex pattern to match your text properly, but that's the gist of it.






share|improve this answer














It's because you're splitting on spaces, so all of the text gets split into list items as well. If the formatting is consistent, I'd suggest splitting on list indices:



>>> line = "[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC"
>>> splitline = line.split()
>>> longitude = splitline[0].replace('[', '').replace(',', '')
>>> latitude = splitline[1].replace(']', '')
>>> tweetWordList = ' '.join(splitline[5:])


Alternatively, you could do it with a regex pattern:



>>> import re
>>> latitude, longitude, tweetWordList = re.findall("^[([d.]+), ([d-.]+)] [d] [d]4-[d]2-[d]2 [d]2:[d]2:[d]2 ([A-Za-z0-9 .']+)", line)[0]


You'll need to play around with the regex pattern to match your text properly, but that's the gist of it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 22:42

























answered Nov 12 '18 at 22:25









Tim

1,744620




1,744620











  • Thank you for the help
    – Marco Aiello
    Nov 12 '18 at 22:46
















  • Thank you for the help
    – Marco Aiello
    Nov 12 '18 at 22:46















Thank you for the help
– Marco Aiello
Nov 12 '18 at 22:46




Thank you for the help
– Marco Aiello
Nov 12 '18 at 22:46

















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53270944%2fassigning-variables-to-parts-of-a-text-file%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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党