Reputation: 55
am new to Python , But i have text File like :
12345 | 6789 | abcd | efgh
i want my output be Like :
12345
6789
abcd
efgh
=====================
i really don't know the script but i made a lot of scripts by those function split() , strip() , blame blame blame
but i failed to do it so am asking for help is someone can .
i will appreciate any Help .
with open('contacts_index1.txt') as f:
lines = f.read().splitlines("|")
Upvotes: 2
Views: 4771
Reputation: 2378
From all of your comments, it looks like the issue has to do with the actual text in the file, and not the ability to parse it. It looks like everyone's solution in here is on the right track, you just need to force the encoding.
The error you are describing is described in this other StackOverflow post.
with open('contacts_index1.txt', 'r') as f:
lines = f.read().encode("utf-8").replace("|", "\n")
EDIT: The issue appears to be a nasty character that wasn't properly decoding. With open
you can tell it to ignore characters it can't decode.
import io
with io.open("contacts_index1.txt", errors="ignore") as f:
lines = f.read()replace("|", "\n")
Upvotes: 1
Reputation: 103694
Please do this line by line. There is no need to read the entire file at once.
Something like:
with open(file_name) as f_in:
for line in f_in:
for word in line.split('|'):
print word.strip()
If it is a unicode issue, most of the time it is automatic:
$ cat /tmp/so.txt
12345 | 6789 | abcd | éfgh
(note the é
in the file)
The program above works. If it does NOT work, use a codec:
with open(fn) as f_in:
for line in f_in:
line=line.decode('utf-8') # or whatever codec is used for that file...
for word in line.split('|'):
print word.strip()
With Python3, just set the encoding when you open the file:
with open(fn, encoding='utf-8') as f_in: # <= replace with the encoding of the file...
for line in f_in:
for word in line.split('|'):
print(word.strip())
Upvotes: 0
Reputation: 638
You will have to use decode. The following code will work:
def dataFunction(filename):
with open(filename, encoding="utf8") as f:
return f.read()
Call this function with filename as parameter:
Contents = dataFunction(filename)
elements = Contents.split("|")
for element in elements:
print(element)
Upvotes: 1
Reputation: 143
I strongly suggest using csv module for this kind of task, as it seems like a csv-type file, using '|' as delimiter:
import csv
with open('contacts_index1.txt','r') as f:
reader=csv.reader(f,delimiter='|')
for row in reader:
#do things with each line
print "\n".join(row)
Upvotes: 0
Reputation: 307
Some problems with the code you posted:
f.read
doesn't read the whole line. It should be f.readline()
. splitlines
?Your question is pretty unclear in differnt aspects. Maybe this snippet could be of some help:
for line in open('contacts_index1.txt'):
elements = line.split('|')
for element in elements:
print element.strip()
Editted: I didn't know the function splitlines
. Just looked it up. The way you used it in your code doesn't seem to be correct anyway.
Upvotes: 0