user7491773
user7491773

Reputation:

Extract substrings from a text file on each line?

Is there a way to extract substrings from a textfile from each like e.g. Say this is the text file but with alot more lines like this:

president, Donald Trump, 74, USA

Priminster, Boris Johnson, 56, UK

I would need to loop through each line and get substrings which are split by commas. So the that the substring would be Donald Trump, 74 and so on for the other lines.

Upvotes: 1

Views: 2818

Answers (5)

Mohak Gangwani
Mohak Gangwani

Reputation: 104

Try this:

lst = []
with open("textfile.txt", "r") as file:
  for line in file:
    stripped_line = line.strip()
    #to save it as a list
    lst.append(stripped_line.split(",")[1:-1])
print(lst)

#to print each of the element
for i in lst:
    print(",".join(i))

Upvotes: 0

plum 0
plum 0

Reputation: 721

Open the file, read the file line by line, then use pythons string.split method with a delimiter of a comma to get a list of words you can filter through.

with open('filename.txt', 'r') as my_file:
    line = my_file.readline()
    while line:
        word_list = line.split(',')
        print(f'{word_list[1]}, {word_list[2]}')
        line = my_file.readline()
    

Upvotes: 0

Abhishek Bhagate
Abhishek Bhagate

Reputation: 5766

You could do it easily using simple split() and join() methods of string in python -

Working Code -

# You could open your file like this
#file1 = open('myfile.txt', 'r') 

# For now I am assuming your file contains the following line of data. 
# You could uncomment above line and use.

file1 = ['president, Donald Trump, 74, USA','president, Donald Trump, 74, USA']
for line in file1: 
    print("".join(line.split(',')[1:3]))

Output :

Donald Trump, 74
Donald Trump, 74

Explanation

  • Basically you are just splitting the string ( each line in file ) at comma and converting the string into array. So line.split(',') will give -

     ['president', ' Donald Trump', ' 74', ' USA']
    
  • Now, we are just joining the 2nd and the 3rd element of the list obtained in the above step. This is done by ",".join() which will join each elements of list with ','.

  • Also, note that we have used [1:3] which will select only the 1st and the 2nd element from the list. So they will give the result which is displayed above

Hope this helps !

Upvotes: 0

Balaji Ambresh
Balaji Ambresh

Reputation: 5037

Here you go:

with open('data.file') as f:
    for line in f:
        parts = line.split(', ')
        if len(parts) == 4:
            print(', '.join(parts[1:3]).strip())

Output:

Donald Trump, 74
Boris Johnson, 56

Upvotes: 1

nagyl
nagyl

Reputation: 1644

You can use split, for splitting string at a specific character. You will get a list, that you can join later on. Reading a file is easy.

with open('filename.txt', 'r') as rf:
    lines = rf.readlines()

For this specific example you can do

for line in lines:
    line = line.strip()
    row  = "{}, {}".format(line.split(',')[1], line.split(',')[2])
    print(row)

Otherwise, please be more clear about what you would like to achieve.

Upvotes: 0

Related Questions