简体   繁体   中英

Splitting a text file into integers in Python

with open("data1.txt", newline='') as f:
    r = csv.reader(f)
    s = list(r)

    dataList = []

I have a data file containing rows of binary integers like this:

00000 0
00001 0
00010 0

etc.

I want to be able to split them all into individual integers so I get the exact same list but all split up into integers:

['0','0','0','0','0','0']
['0','0','0','0','1','0']
['0','0','0','1','0','0']

etc then append each one to dataList. So far I can split them up to the space in between them but that's it

This probably the simplest solution. Remember that a string is iterable.

datalist = []
with open("data1.txt") as f:
    for line in f:
        line = line.replace(" ", "")
        line_list = list(line.strip())
        datalist.append(line_list)

list(string) will give you a list of the characters in the string.

Assuming data1.txt as

00000 0
00001 0
00010 0

you can write

with open('data1.txt') as f: 
    result = [list(line.strip().replace(' ', '')) for line in f] 

to get

>>> result                                                                 
[['0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '1', '0'],
 ['0', '0', '0', '1', '0', '0']]

This regex will work:

import re
digits = "000001"
re.sub(r'([0-9])(?!$)', r'\1,', digits)

Instead of digits above just pass every line of your file

If you can use a 3rd party library, you can do this with Pandas:

import pandas as pd
from io import StringIO

x = StringIO("""00000 0
00001 0
00010 0""")

# replace x with 'file.csv'
df = pd.read_csv(x, delim_whitespace=True, dtype=object, header=None)

res = [[*x, y] for x, y in df.values]

Result:

[['0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '1', '0'],
 ['0', '0', '0', '1', '0', '0']]

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