简体   繁体   English

删除txt文件中的换行符

[英]Removing newline characters in a txt files

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. 我正在做欧拉问题,并在问题8出现 ,想将这个巨大的1000位数字复制到numberToProblem8.txt文件中,然后将其读入我的脚本中,但是我找不到从其中删除换行符的好方法它。 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. 我使用print()仅检查它是否正常工作,但没有。 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. 我使用strip()删除了所有垃圾,然后使用join()将已清理的行添加到hugeNumberAsStr(需要使用字符串来连接这些行,以后再使用int()),并对所有行重复该操作。 Here is the .txt file with a number in it. 这是带有数字的.txt文件。

You need to do hugeNumberAsStr += aSingleLine instead of hugeNumberAsStr.join(..) 您需要执行hugeNumberAsStr += aSingleLine而不是hugeNumberAsStr.join(..)

str.join() joins the passed iterator and return the string value joined by str . str.join()联接传递的迭代器,并返回由str联接的字符串值。 It doesn't update the value of hugeNumberAsStr as you think. 它不会像您所想的那样更新hugeNumberAsStr的值。 You want to create a new string with removed \\n . 您想创建一个删除了\\n的新字符串。 You need to store these values in new string. 您需要将这些值存储在新字符串中。 For that you need append the content to the string 为此,您需要将内容附加到字符串

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

The join method for strings simply takes an iterable object and concatenates each part together. 字符串的join方法只需要一个可迭代的对象并将每个部分连接在一起。 It then returns the resulting concatenated string. 然后,它返回结果连接字符串。 As stated in help(str.join): 如help(str.join)中所述:

join(...) S.join(iterable) -> str join(...)S.join(可迭代)-> 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. 因此,join方法实际上并不能满足您的要求。 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. 这消除了执行剥离的多余代码行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM