Skip to main content
本文に 79 文字追加
ソース リンク
metropolis
  • 6,705
  • 2
  • 6
  • 16

format_data.txt

test login_id: userid
authority: root folder: root123
login password: 1234***

Python script

import re

textwith =open('format_data.txt', 'r'''
test) login_id:as userid
authorityf: 
 root folder: root123
login password: 1234***
'''.lstrip()

data = [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', textf.read())] 

print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]
import re

text = '''
test login_id: userid
authority: root folder: root123
login password: 1234***
'''.lstrip()

data = [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', text)]
print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]

format_data.txt

test login_id: userid
authority: root folder: root123
login password: 1234***

Python script

import re

with open('format_data.txt', 'r') as f: 
    data = [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', f.read())] 

print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]
本文から 55 文字削除
ソース リンク
metropolis
  • 6,705
  • 2
  • 6
  • 16
import re
import io

text = '''
test login_id: userid
authority: root folder: root123
login password: 1234***
'''.lstrip()

data = []
for l in io.StringIO(text):
    data += [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', ltext)]
 
print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]
import re
import io

text = '''
test login_id: userid
authority: root folder: root123
login password: 1234***
'''.lstrip()

data = []
for l in io.StringIO(text):
    data += [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', l)]
 
print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]
import re

text = '''
test login_id: userid
authority: root folder: root123
login password: 1234***
'''.lstrip()

data = [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', text)]
print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]
本文から 9 文字削除
ソース リンク
metropolis
  • 6,705
  • 2
  • 6
  • 16
import re
import io

text = '''
test login_id: userid
authority: root folder: root123
login password: 1234***
'''.lstrip()

data = []
for l in io.StringIO(text):
    data += [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', l.rstrip())]

print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]
import re
import io

text = '''
test login_id: userid
authority: root folder: root123
login password: 1234***
'''.lstrip()

data = []
for l in io.StringIO(text):
    data += [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', l.rstrip())]

print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]
import re
import io

text = '''
test login_id: userid
authority: root folder: root123
login password: 1234***
'''.lstrip()

data = []
for l in io.StringIO(text):
    data += [{k: v} for k, v in re.findall(r'\s*(.+?):\s+([^\s]+)', l)]

print(data)

# [{'test login_id': 'userid'}, {'authority': 'root'}, {'folder': 'root123'}, {'login password': '1234***'}]
ソース リンク
metropolis
  • 6,705
  • 2
  • 6
  • 16
読み込んでいます