0

I'm doing Euler Problems and am at problem #8 and wanted to just copy this huge 1000-digit number to a numberToProblem8.txt file and then just read it into my script but I can't find a good way to remove newlines from it. With that code:

hugeNumberAsStr = ''

with open('numberToProblem8.txt') as f:
    for line in f:
        aSingleLine = line.strip()
        hugeNumberAsStr.join(aSingleLine)

print(hugeNumberAsStr)

Im using print() to only check if it works and well, it doesnt. It doesnt print out anything. What's wrong with my code? I remove all the trash with strip() and then use join() to add that cleaned line into hugeNumberAsStr (need a string to join those lines, gonna use int() later on) and its repeated for all the lines. Here is the .txt file with a number in it.

doublemc
  • 3,021
  • 5
  • 34
  • 61

3 Answers3

1

You need to do hugeNumberAsStr += aSingleLine instead of hugeNumberAsStr.join(..)

str.join() joins the passed iterator and return the string value joined by str. It doesn't update the value of hugeNumberAsStr as you think. You want to create a new string with removed \n. You need to store these values in new string. For that you need append the content to the string

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • @doublemc `+=` will add the string to the previous part. `join` is more used to combine together existing string elements in a list or other similar object. – David542 Dec 14 '16 at 23:30
  • @doublemc: Check edit. Feel free to ask in case of more doubt. I will suggest to go through the document once. You will get more information – Moinuddin Quadri Dec 14 '16 at 23:31
  • 1
    I get it now. If I wanted to I could use append() but according to this post: http://stackoverflow.com/a/12171382/7227475 += is better and I will be using this one in situations like this, thank you! – doublemc Dec 14 '16 at 23:54
1

What about something like:

hugeNumberAsStr = open('numberToProblem8.txt').read()
hugeNumberAsStr = hugeNumberAsStr.strip().replace('\n', '')

Or even:

hugeNumberAsStr = ''.join([d for d in hugeNumberAsStr if d.isdigit()])

I was able to simplify it to the following to get the number from that file:

>>> int(open('numberToProblem8.txt').read().replace('\n',''))
731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544
David542
  • 104,438
  • 178
  • 489
  • 842
1

The join method for strings simply takes an iterable object and concatenates each part together. It then returns the resulting concatenated string. As stated in help(str.join):

join(...) S.join(iterable) -> str

Return a string which is the concatenation of the strings in the
iterable.  The separator between elements is S.

Thus the join method really does not do what you want. The concatenation line should be more like:

hugeNumberAsString += aSingleLine

Or even:

hugeNumberAsString += line.strip()

Which gets rid of the extra line of code doing the strip.

John F
  • 176
  • 4