简体   繁体   中英

Python - Format file to list

I've read some questions on how to pass a file to a list, but this file it's a bit trickier as there are different data types in it, all of them represented as a string in that file.

I've managed to get a file that looks like this:

['verb', 0, 5, 7]['noun', 9, 3, 4]

How can I turn this into a list that looks like:

list = [['verb', 0, 5, 7], ['noun', 9, 3, 4]]

where 'verb' and 'noun' are strings and all numbers are integers.

If you mean that the file consists of groups enclosed in square brackets, all in one line, then probably the best idea is to replace all ][ sequences by ],[ sequences so that your data becomes valid JSON, then parse it using json.loads :

import json
with open('myfile', 'r') as f:
    line = f.readline().rstrip()
list_of_lists = json.loads("[" + line.replace('][', '],[').replace("'", '"') + "]")

You can try this:

data = open('file.txt').read().strip('\n')
import re
lists = re.findall("\[(.*?)\]", data)
final_list = [[int(i) if i.isdigit() else i[1:-1] for i in b.split(", ")] for b in lists]

You could use regex for this task. Below you can find a sketch of how to apply this technique here:

import re

s = "['verb', 0, 5, 7]['noun', 9, 3, 4]"

#create the regex expression
pattern = re.compile(r'\[(.*?)\]')

#store the data here
result = []

#get every item entry using the regex expression
for x in re.findall(pattern, s):
    z = x.split(",")
    #parse the data entries
    result.append([z[0].replace("'", ""), int(z[1]), int(z[2]), int(z[3])])

result

>>>[['verb', 0, 5, 7], ['noun', 9, 3, 4]]

I would open the file, then read it, store in a string and replace all "]" with "],". Then I'd eval (yeah I know it's evil but...) that string and would convert it to a list.

with open('your_file.txt', 'r') as raw_file:
    your_str = raw_file.read()
your_str = your_str.replace('][', '],[')
your_list = list(eval(your_str))

If you know that the content of the file will be JSON, you can use json.loads instead of eval . In the example you gave above, you should convert ' to " in order to being valid JSON.

Follow this steps:

  • Identify who is storing data in this no format
  • Call them
  • Talk with this person about benefits to store data in an standard format. They are some available formats: csv, json, xml.
  • Ask them about to receive data in this format.
  • Deserialize data from file in one line library utility. Like `json.load('your_file.txt')

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