Search code examples
pythonstringcsvwordsfindall

How to print words from a string up to a point in Python


I have created a script in Python which prints the word following a certain string in a csv file.

The code I have is:

    `import re
    file=open("csvfile.csv", "r")
    text=file.read()
    data=re.findall('(?<=originalName\=\')\w+',text)
    print data`

This works, however I want to print the words up until the character " in the string. Currently it only prints out the first word and the originalName could range from one to many words.

Thanks


Solution

  • You can use this regex. Which matches all characters until "

    re.findall("(?<=originalName\=\')[^\"]*",text)