简体   繁体   中英

Python 2 - How do I import a text file containing a long sequence of digits and convert it to a string of individual numbers?

I want to take a text file that is a long sequence of digits with line breaks, ie something like

38482406847387
85869153438194
96531040384827
43157689643163

but much larger, and convert it to a string that would just read

[3,8,4,8,...,1,6,3]

so that I can iterate over it, manipulate it, visualise it and so on.

I have had a look at the open() function but so far I can only get it to break up the file into separate lines. I know I can use a for loop to go through the giant string of the whole document and form a list that way, but then I get '/n' and spaces showing up everywhere, which is undesirable.

For context, I grabbed a text file from the web of some preposterous number of digits of pi, and I thought it would be instructive and interesting to go through it and look for patterns, plot the distribution of digits, convert to ASCII and other such nonsense. I figured it would be a fun way for me to learn a bit more about Python.

import re

print re.findall('\d', open('file.txt', 'r').read())

For small files:

with open('path/to/file') as infile:
    answer = list(int(i) for i in ''.join(line.strip() for line in infile))

For larger files:

answer = []
with open('path/to/file') as infile:
    for line in infile:
        answer.extend([int(i) for i in line.strip()])

If you get all the numbers as a string then you can use

# here, digits is the numbers as a string including the \n character
list = [digit for digit in digits.replace('\n', '')]
with open("/path/to/file") as f:
    print [int(x) for x in f.read() if x.isdigit()]

This is shorter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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