0

I'm not able to get the correct value from txt file onto dictionary list. I'm calling raw output of text file, read it line by line, remove white space and parse onto dictionary.

Please find txt file below which consists of product id and product name. Please take note, some of the data content in the file source is upside down..what i mean is product id come first before product name (not in sequence)...pls refer to sample data below

Product id: sq112
Prodname: ment-bar1
Product id: sq001
Prodname: jumw-cd12
Product id: mcc-hg921
Prodname: emq-vx02
Product id: mmc112
Prodname: ment-bar2
Product id: cx022
Prodname: mxx-21ed
Product id: vcb113
Prodname: emq-vx05
Prodname: trc-vc01
Product id: emx-21ee

This is the script use to get the id and name

file = open("prd1.txt")
data = file.readlines()
dict = {"ProdList":[]}
temp_dict ={}

for line in data:
    line = line.replace('\n', '').strip()
    line = line.split(':')
    line = list(filter(None,line))
    temp_dict["ID"] = line[0]
    temp_dict["Name"] = line[1]
    dict["ProdList"].append(temp_dict)
    temp_dict = {}
print(dict)

The dict output generated as below

{'ProdList': [{'ID': 'Product id', 'Name': ' sq112'}, {'ID': 'Prodname', 
'Name': ' ment-bar1'}, {'ID': 'Product id', 'Name': ' sq001'}, {'ID': 
'Prodname', 'Name': ' jumw-cd12'}, {'ID': 'Product id', 'Name': ' mcc- 
hg921'}, {'ID': 'Prodname', 'Name': ' emq-vx02'}, {'ID': 'Product id', 
'Name': ' mmc112'}, {'ID': 'Prodname', 'Name': ' ment-bar2'}, {'ID': 
'Product id', 'Name': ' cx022'}, {'ID': 'Prodname', 'Name': ' mxx-21ed'}, 
{'ID': 'Product id', 'Name': ' vcb113'}, {'ID': 'Prodname', 'Name': ' emq- 
vx05'}]}

Expected output as follows

{'ProdList':[{'ID':'sq112','Name':' ment-bar1'},{'ID':'sq001','Name':' 
jumw-cd12'},{'ID':'mcc-hg921','Name':' emq-vx02'}]}

I tried as suggested below

for i in range(0,len(data),2):
  line = data[i].split(':')
  nxt_line = data[i+1].split(':')
  if 'id' in data[0]:
    dict['ProdList'].append({'ID':line[1], 'Name': nxt_line[1]})
  else:
    dict['ProdList'].append({'ID':nxt_line[1], 'Name': line[1]})

Im getting output below

{'ProdList':[{'ID':' sq112\n','Name':' ment-bar1\n'},{'ID':' 
sq001\n','Name':' jumw-cd12\n'},{'ID':' mcc-hg921\n','Name':' emq-vx02\n'}, 
{'ID':' mmc112\n','Name':' ment-bar2\n'},{'ID':' cx022\n','Name':' mxx- 
21ed\n'},{'ID':' vcb113\n','Name':' emq-vx05\n'},{'ID':' trc- 
vc01\n','Name':' emx-21ee  \n'}]}

The last id and product name upside down... it should be ID: emx-21ee and Name: trc-vc01

1
  • Hi..all..the solution below works but im facing another issues where some of the data source from the file did not come in sequence product id and product name.... Thus...when run the code the values could be mix up ...and not parse correctly onto the dictionary... Commented Apr 11, 2019 at 2:49

2 Answers 2

2

Try this for the files where product id comes before product name :

file = open("prd1.txt")
data = file.readlines()
data = [i.strip() for i in data]
dict_ = {"ProdList":[]}
for i in range(0,len(data),2):
    line = data[i].split(':')
    nxt_line = data[i+1].split(':')
    dict_['ProdList'].append({'ID':line[1], 'Name': nxt_line[1]})

OUTPUT :

{'ProdList': [{'ID': ' sq112', 'Name': ' ment-bar1'}, {'ID': ' sq001', 'Name': ' jumw-cd12'}, {'ID': ' mcc-hg921', 'Name': ' emq-vx02'}, {'ID': ' mmc112', 'Name': ' ment-bar2'}, {'ID': ' cx022', 'Name': ' mxx-21ed'}, {'ID': ' vcb113', 'Name': ' emq-vx05'}]}

If for some files, product name comes before product id, you'll have to modify inside the for loop part where we assign values to the 'ID' and 'Name' keys. This change requires prerequisite knowledge whether the files contain product name before product id. Here is the code :

for i in range(0,len(data),2):
    line = data[i].split(':')
    nxt_line = data[i+1].split(':')
    dict_['ProdList'].append({'ID':nxt_line[1], 'Name': line[1]})  # Change here

If you want to make the whole process unanimously automated i.e. you don't have the knowledge which files contain product name before product id and which one opposite way, you have to check whether the first line contains the keyword id or name with :

if 'id' in data[0]:
    # proceed with the first loop
else:
    # proceed with the second loop
Sign up to request clarification or add additional context in comments.

2 Comments

Hi sir...this solution works...but some of the data content in the file source is upside down..what i mean is product id come first before product name...pls refer to sample of file data above... when I run the data for this part will be upside down... ID will be the product name and Name will be product id.... I noticed some of the data from this file is generated this way.... meaning that not all will start from product id and product name.,,, some will have product name first than product id... How to ensure product ID value and Name value is correctly parse. Please advise.. thanks
Hi sir..thanks for your response... the source data is on a single file.... which contain product id and productname or productname and product id...which is not in sequence... the problem here happen in a single file... the first line start ok...product id and productname...but maybe on the 6th line its start opposite with productname follow by product id and continue back with product id and productname.....
1

You can pair every two lines by zipping the file generator with itself:

with open('prd1.txt') as file:
    print({'ProdList': [{'ID': id.split(': ')[1].rstrip(), 'Name': name.split(': ')[1].rstrip()} for id, name in zip(data, data)]})

This outputs:

{'ProdList': [{'ID': 'sq112', 'Name': 'ment-bar1'}, {'ID': 'sq001', 'Name': 'jumw-cd12'}, {'ID': 'mcc-hg921', 'Name': 'emq-vx02'}, {'ID': 'mmc112', 'Name': 'ment-bar2'}, {'ID': 'cx022', 'Name': 'mxx-21ed'}, {'ID': 'vcb113', 'Name': 'emq-vx05'}]}

1 Comment

Thank you for your solution sir, but still got issue of data source file above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.