Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert TXT to CSV
#1
Hello,

I need help converting a text file, to a CSV that I could use in splunk.

This is my source file:

Image Digest: sha256:316ebb92ca66bb8ddc79249fb29872bece4be384cb61b5344fac4e84ca4ed2b2
Full Tag: docker.io/debian:latest
Image ID: 2b98c9851a37e18356abe407922726dc35c370f7cee76f435f94ee92950b4493
Status: fail
Last Eval: 2018-03-29T07:39:35
Policy ID: 94a030d0-241f-4ad9-8ef6-a74ee8d2aa42

Gate Trigger Detail Status
anchoresec vulnhigh HIGH Vulnerability found in package - libblkid1 (CVE-2016-2779 - https://security-tracker.debian.org/trac...-2016-2779) stop
anchoresec vulnhigh HIGH Vulnerability found in package - libc6 (CVE-2017-8804 - https://security-tracker.debian.org/trac...-2017-8804) stop
anchoresec vulnhigh HIGH Vulnerability found in package - libc6 (CVE-2018-1000001 - https://security-tracker.debian.org/trac...18-1000001) stop

So I would like to keep only the four columns (gate, trigger, detail, status), the TullTag entry as a title, and convert it to CSV (commas and quotation marks for the details column)

How can I proceed in python to get a CSV from the above file?

I don't know if it's any easier butI can also get the source file in json form, but I do have useless information to sort. This is an example:

"result": {
"2b98c9851a37e18356abe407922726dc35c370f7cee76f435f94ee92950b4493": {
"result": {
"final_action": "STOP",
"header": [
"Image_Id",
"Repo_Tag",
"Trigger_Id",
"Gate",
"Trigger",
"Check_Output",
"Gate_Action",
"Whitelisted"
],
"row_count": 29,
"rows": [
[
"2b98c9851a37e18356abe407922726dc35c370f7cee76f435f94ee92950b4493",
"docker.io/debian:latest",
"CVE-2016-2779+libblkid1",
"anchoresec",
"vulnhigh",
"HIGH Vulnerability found in package - libblkid1 (CVE-2016-2779 - https://security-tracker.debian.org/trac...-2016-2779)",
"stop",
false
],
[
"2b98c9851a37e18356abe407922726dc35c370f7cee76f435f94ee92950b4493",
"docker.io/debian:latest",
"CVE-2017-8804+libc6",
"anchoresec",
"vulnhigh",
"HIGH Vulnerability found in package - libc6 (CVE-2017-8804 - https://security-tracker.debian.org/trac...-2017-8804)",
"stop",
false
]

Thank you in advance.

Sincerely. Smile
Reply
#2
(Mar-29-2018, 08:14 AM)jabberwock88 Wrote: I need help converting a text file, to a CSV that I could use in splunk.
Great. What have you tried? We are glad to help, but we are not going to do it for you. Post your code in python tags, if you get an exception post full traceback in error tags and ask specific questions.

You may want to check our tutorial on working with files - https://python-forum.io/Thread-Basic-Files

In my opinion it would be easier to use the json file as input (not that txt is a problem). Check json module from Python Standard Library
For writing - you will find csv module from Standard library useful.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020