greyoxide
greyoxide

Reputation: 1287

python read text file into array

I am trying to use Python to auto-parse a set of text files and turn them into XML files.

There are alot of people asking how to loop through a text file and read them into an array. The trouble here is that this wont quite work for me.

I need to loop through the first three lines individually then drop the rest of the text file (body) into one array entry.

The text file is formatted as follows.

Headline

Subhead

by A Person

text file body content. Multiple paragraphs

How would I go about setting up an array to do this in Python?

Upvotes: 0

Views: 3109

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251186

Something like this:

with open("data1.txt") as f:
    head,sub,auth = [f.readline().strip() for i in range(3)]
    data=f.read()
    print head,sub,auth,data

If you've spaces between the the lines, then you should try:

filter() will remove he empty lines:

 with open("data1.txt") as f:
    head,sub,auth =filter(None,(f.readline().strip() for i in range(6)))
    data=f.read()
    print head,sub,auth,,data

Upvotes: 2

Jeff Gortmaker
Jeff Gortmaker

Reputation: 4737

If I understood your question correctly, you wish to put all the text except for the first 3 lines into an array (list). Here's how to do that:

with open("/path/to/your/file.txt") as f:
    all_lines = f.readlines()
content_lines = all_lines[3:]
content_text = '\n'.join(content_lines)
content_list.append(content_text)

Explanation: You first open the file, and then put all of its lines into a list. Then, you take all the lines after the first three, and put those into a list. Then, you join this new list with newlines to make it content again. Then, you append this new content to a list that you've created beforehand called content_list


If you want to put the first three lines into your list as well, then do the following before appending to content_list:

for line in all_lines[:3]:
    content_list.append(line)

Upvotes: 1

Related Questions