find the number of arrays containing common terms for txt and csv

Asked 1 years ago, Updated 1 years ago, 407 views

I am writing a program to find the number of arrays in the 1.txt array. I have now used the count method to output the number of terms in 1.txt. However, I would like to find the number of arrays containing terms, not the number of terms. 2.csv

first, 2,1234
second, 1,2345

source code

defmain():
    # Array of CSVs with IDs and words on each line
    with open('1.csv', 'r') as a:
        rows=a.readlines()
    a.close()

    # put the entire sentence someone looks for in a search for
    with open('1.txt', 'r') as b:
        text=b.read()
        b.close()

    
    id_count = {}

    with open('2.csv', 'w') as c:
        c.write('term, number of times, id\n')
        For row in rows:
            # Divide the string id, word into
            tmp = row.split(',')
            id=tmp[0]
            # remove line breaks and lowercase all csv terms
            word=tmp[1].trip().lower()
            # lowercase text and count how many times word is in text
            count=text.lower().count(word)
            # If text does not have a common term, it will not be printed.
            if count == 0:
                pass
            else:
                c.write('%s, %d, %s\n'%(word, count, id))
            
    c.close()

if__name__=='__main__':
    main()

1.txt

 ['I was first.', 'I was first.']
['I was first.', 'I was second.']

1.csv

1234, first
2345, second

python python3

2022-12-20 09:21

2 Answers

"I don't know what the questioner means by ""array,"" but I interpreted it as a double loop implementation by checking if each line of 1.txt contains 1.csv words ("first", "second").

defmain():
    # Array of CSVs with IDs and words on each line
    with open('1.csv', 'r') as a:
        rows=a.readlines()

    # put the entire sentence someone looks for in a search for
    with open('1.txt', 'r') as b:
        text=b.read()

    text=text.split("\n")
    with open('2.csv', 'w') as c:
        c.write('term, number of times, id\n')
        For row in rows:
            row=row.strip()
            count = 0
            Fort in text:
                t=t.lower()
                ifrow.trip().split(",")[1] int:
                    count = count +1
            c.write(", ".join([row.split(", ")[1], str(count), row.split(", ")[0]])))
            c.write("\n")
                    
if__name__=='__main__':
    main()


2022-12-20 09:24

Some changes have been made, such as the loading part of the file.

import csv
importio

with io.StringIO(f1_csv) as fp:
    rows=list(csv.reader(fp))
withio.StringIO(f1_txt) as fp:
    text=fp.read()

lst=text.split('\n')# [ast.literal_eval(p)for pin text.split('\n')]

with open('2.csv', 'w') as c:
    c.write('term, number of times, id\n')
    For row in rows:
        cnt=sum(row[1] in v for vinst)
        c.write('{}, {}, {}, {}\n'.format(row[1], cnt, row[0]))

!cat2.csv
Terminology, number of times, id
first, 2,1234
second, 1,2345


2022-12-20 09:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.