Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据其他列表中的字符串,仅输出txt文件中的特定项_Python_List_Text Files - Fatal编程技术网

Python 根据其他列表中的字符串,仅输出txt文件中的特定项

Python 根据其他列表中的字符串,仅输出txt文件中的特定项,python,list,text-files,Python,List,Text Files,我有一个字符串列表: myStrings = [Account,Type, myID] 我还有一个txt文件,其中包含与这些字符串关联的数字,例如: [90: 'Account', 5: 'Type', 6: 'MyID', 8: 'TransactionNum', 9: 'Time'] 如何仅打印txt文件中myStrings中的数字和字符串。例如,由于MyString中没有时间,因此我不想打印时间。我还想把这个txt文件列为一个列表在您说该文件没有[&]之后,可以使它像这样工作: imp

我有一个字符串列表:

myStrings = [Account,Type, myID]
我还有一个
txt文件
,其中包含与这些字符串关联的
数字,例如:

[90: 'Account', 5: 'Type', 6: 'MyID', 8: 'TransactionNum', 9: 'Time']

如何仅打印txt文件中myStrings
中的
数字和字符串。例如,由于MyString中没有时间,因此我不想打印时间。我还想把这个
txt文件列为一个列表

在您说该文件没有
[
&
]
之后,可以使它像这样工作:

import json
myStrings = ['Account','Type', 'myID']

with open('text-file.txt') as filename:
  file_text = filename.read()


file_text_list = file_text.split(',')
file_text_dict = {}
for item in file_text_list:
  k, v = item.split()
  v = v.replace("'", "")
  k = k.replace(":", "")
  if v in myStrings:
    file_text_dict[k] = v
print(file_text_dict)  # output => {'90': 'Account', '5': 'Type'}
print(list(file_text_dict.values()))  # output => ['Account', 'Type']

在您说该文件没有
[
&
]
之后,可以使其这样工作:

import json
myStrings = ['Account','Type', 'myID']

with open('text-file.txt') as filename:
  file_text = filename.read()


file_text_list = file_text.split(',')
file_text_dict = {}
for item in file_text_list:
  k, v = item.split()
  v = v.replace("'", "")
  k = k.replace(":", "")
  if v in myStrings:
    file_text_dict[k] = v
print(file_text_dict)  # output => {'90': 'Account', '5': 'Type'}
print(list(file_text_dict.values()))  # output => ['Account', 'Type']
这将有助于你:

myStrings = ['Account','Type', 'myID']
f = open("D:\\Test.txt","r")
txt = f.read()
f.close()

txt = txt.replace('\n',' ')

txt = txt.split(',')

txtlst = []
for x in txt:
    txtlst.append(x.split(':'))

numslst = [int(txtlst[i][0]) for i in range(len(txtlst))]

strlst = []

for i in txtlst:
    for j in i:
        try:
            int(j)
        except ValueError:
            strlst.append(j.replace("'",""))

for x in range(len(strlst)):
    strlst[x] = strlst[x].replace(' ','')

for x in range(len(strlst)):
    if strlst[x] in myStrings:
        print(numslst[x])
        print(strlst[x])
输出:

90
Account
5
Type
这将有助于你:

myStrings = ['Account','Type', 'myID']
f = open("D:\\Test.txt","r")
txt = f.read()
f.close()

txt = txt.replace('\n',' ')

txt = txt.split(',')

txtlst = []
for x in txt:
    txtlst.append(x.split(':'))

numslst = [int(txtlst[i][0]) for i in range(len(txtlst))]

strlst = []

for i in txtlst:
    for j in i:
        try:
            int(j)
        except ValueError:
            strlst.append(j.replace("'",""))

for x in range(len(strlst)):
    strlst[x] = strlst[x].replace(' ','')

for x in range(len(strlst)):
    if strlst[x] in myStrings:
        print(numslst[x])
        print(strlst[x])
输出:

90
Account
5
Type

你能举例说明这个txt文件包含什么吗?如果文本文件是由您创建的,则创建为json,以便轻松执行您需要的任务如果您确实希望以文本文件的形式处理,则必须读取将成为字符串的内容,并且需要应用字符串拆分或regext。txt文件如下所示:831:“Account”,30:“键入”文本文件中是否有
90:“帐户”
'5':“键入”
等单独的行??或者你把它和你展示的一模一样??如果将
[…]
作为单行,您能否详细显示此txt文件包含的内容?如果文本文件是由您创建的,则创建为json,以便轻松执行您需要的任务如果您确实希望以文本文件的形式处理,则必须读取将成为字符串的内容,并且需要应用字符串拆分或regext。txt文件如下所示:831:“Account”,30:“键入”文本文件中是否有
90:“帐户”
'5':“键入”
等单独的行??或者你把它和你展示的一模一样??将
[…]
作为单行??