Can anyone explain this code

with open(“csv”, “rU”) as f:
reader = csv.reader(f)
my_headers = reader.next()
for row in reader:
# print row[1]
report = row[1]
sentences = report.split(’.’)
for i in sentences:
words = i.split(’ ')
i = ’ ‘.join(i.split())
words = i.lstrip().rstrip().split(’ ')
for j in range(len(words) - 1):
pairs[words[j] + ’ ’ + words[j + 1]] = pairs.get(words[j] + ’ ’ + words[j + 1], 0) + 1

1 Like

Hi @selb,

So that others can read and copy your code in order to test and experiment with it, you should format the code when you post it. See Quick Tips for Writing Good Posts, which includes a section entitled Format Your Code.

Your Python program opens and reads a file named "csv" that contains comma-separated values, including text. Could you post the contents of that file, or part of it, if it is very large, so that we can read it with your Python code?

1 Like

found in the RAW modus of the topic…

with open("csv", "rU") as f:
    reader = csv.reader(f)
    my_headers = reader.next()
    for row in reader:
        # print row[1]
        report = row[1]
        sentences = report.split('.')
        for i in sentences:
            words = i.split(' ')
            i = ' '.join(i.split())
            words = i.lstrip().rstrip().split(' ')
            for j in range(len(words) - 1):
                pairs[words[j] + ' ' + words[j + 1]] = pairs.get(words[j] + ' ' + words[j + 1], 0) + 1
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.