Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i used this code to extracts all the content of every instance of the fields \'a

ID: 3903970 • Letter: I

Question

i used this code to extracts all the content of every instance of the fields 'agents' and 'ir' appeared from the example given, and gives the output shown in example2 ,

import re x = "agent"

filename = "text.txt"

agent=[]

ir=[]

with open(filename, "rt") as in_file:

for line in in_file: line = line.translate(None, ',:""')

if x in line:

#print line[line.find(x)+len(x):]

agent.append((line[line.find(x)+len(x):]).strip())

import re

x="ir"

filename = "text.txt"

with open(filename, "rt") as in_file:

for line in in_file:

line = line.translate(None, ',:""')

if x in line:

#print line[line.find(x)+len(x):]

ir.append((line[line.find(x)+len(x):]).strip())

for i in range(1,len(agent)): print agent[i-1] + "," + ir[i-1]

example text

dgfh lsdkfh lskdjv 3w4534 "agent":"FIVR_VNN_STAP", fghj ddyj dryghdry "agent":"FIVR_VNN_STAP", dryjh fghxrt zaerg zdf "agent":"FIVR_IOCTRL_STAP", "ir":"RTDR_TAP0", "ir":"RTDR_TAP3", "ir":"RTDR_TAP4", JZDHA ASLDJ L ADLKFJ LADJF ASD FSDF AFDSFDFJKSDKFJLZRJF DLFKZG ERJOI AZEODFIH SLDIRF AO DFJLS S DLKFJ LAWER FSDLFJ LJRI FUZERJF SDJKF EFJZ SDKJF ZS RF;WEOJF ;ZSKDJF ;SKDJFLDKJF;SKJR ZSKFJG LZSDJF ZSDJF; J;SKDJF; ;SKDJ;FGZEJR;GODJF; G;ZSLKF;Z SF;G JZS;FJG ;SEORUF LSDKFJ CLSJD FJSDFHK GHKSDGFK SJDGF A

output:

FIVR_VNN_STAP,RTDR_TAP0

FIVR_VNN_STAP,RTDR_TAP3

FIVR_IOCTRL_STAP,RTDR_TAP4

FIVR_1P05_STAP,RTDR_TAP4

FIVR_1P05_STAP,RTDR_TAP

FIVR_1P05_STAP,RTDR_TAP0

How would i write this output to a CVS file?

Explanation / Answer

Here your requirement is bit unclear. As far as my understanding from your question is to write the final output to the file. So here is the code block for the file writing operation.

f= open("output.csv","w+")

for i in range(1,len(agent)):

<Tab space> f.write(agent[i-1] + "," + ir[i-1]+" ")

<Tab space> print agent[i-1] + "," + ir[i-1]

Please comment if you face any difficulty or if you want any modification.