Abhishek jha
Abhishek jha

Reputation: 11

String to dataframe

I am developing QR code scanner to csv. I am very much new in python. I am getting this output after scanning QR 'Employee ID: 101\nEmployee Name: Abhinav Jha\nDesignation: Student\nDepartment: Mechanical'. Can anyone help me to covert it to csv. Thanks in advance.

Upvotes: 1

Views: 32

Answers (1)

Nk03
Nk03

Reputation: 14949

Well, you can convert this incoming string to a python dictionary and then you can easily write the values in a CSV file.

dict_1 = {(item.split(':')[0]).strip():(item.split(':')[1]).strip() for item in s.split('\n')}
print(dict_1)

output -

{'Employee ID': '101',
 'Employee Name': 'Abhinav Jha',
 'Designation': 'Student',
 'Department': 'Mechanical'}

Now, if you wanna access keys then use the .values() function -

print(dict_1.values()) # will print ['101', 'Abhinav Jha', 'Student', 'Mechanical']

I'm assuming you wanna write these values in a CSV file.

Upvotes: 1

Related Questions