简体   繁体   中英

How to convert a data read from a text file to an array of numbers?

I am trying to convert a data read from an textfile to an array of numbers.

I have done the following,

with open(src+"process_ids.txt", "r") as f:
    data = f.readlines()

the output data is,

['[36950, 36968, 36969]']

and the type is of string

I would like to convert it to an array of numbers such as,

[36950, 36968, 36969]

It looks like it's a json format. You can try the following:

import json
with open(src + "process_ids.txt", "r") as f:
    data = json.loads(f.read())

Now, data has a list type.

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