How To Read File When The Words Are Separated By "|" (psv)?
Solution 1:
If you don't mind to use existing framework, you can use pandas. You can skip first row using skiprows=1 and change the separator using sep='|'
# load pandasimport pandas as pd
# read file as pandas dataframe
dataframe = pd.read_csv(file,skiprows=1,sep='|')
print(dataframe)
To install pandas
pip install pandas
Pandas documentation for read_csv
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html
Other option is to use csv reader to read your psv file
import csv
withopen('file.psv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter='|')
next(csv_reader, None) # read once to skip the header oncefor row in csv_reader:
print(row)
Solution 2:
If you use [1:-1]
(I think) you can select a sub array which starts after the first value of the array, which in the case of a file, should mean you get every line except the first.
Solution 3:
if you need to read from second line you can change your code from: for n, line in enumerate(fp, 1)
to for n, line in enumerate(fp[1:], 1)
Solution 4:
If you want an ultra shoddy ++ option to skip enumerating the first value: make a boolean value initialised to true, and then add an if statement at the start of your for loop which tests if this boolean value is true. Inside this if
statement, set the value to false, and then pass a continue
Something like:
b=Truefork,v in enumerator:if b:b=Falsecontinue# Some code
Solution 5:
In order to achieve what you request, the function is fine, and it is important to call it with the correct arguments, and make them different from default.
From the code, the default behavior is to use ,
as a separator, and to not skip the first line of the file. In order to actually split with |
and skip the first line (i.e. a header), then we will set seperator='|'
and header = True
when we call it.
# Function is fine, leave as-is#deffile_reader(path, num_fields, seperator = ',', header = False):
try:
fp = open(path, "r", encoding="utf-8")
except FileNotFoundError:
raise FileNotFoundError("Unable to open file.")
else:
with fp:
for n, line inenumerate(fp, 1):
fields = line.rstrip('/n').split(seperator)
iflen(fields) != num_fields:
raise ValueError("Unable to read file.")
elif n == 1and header:
continueelse:
yieldtuple([f.strip() for f in fields])
# Example file afile.txt contains these lines:# alfa|beta|gamma|delta# 1|2|3|4# a|b|c|d# here we call the function:
filename = 'afile.txt'for x in file_reader(filename, 4, '|', True): #note the separator and headerprint(x)
Post a Comment for "How To Read File When The Words Are Separated By "|" (psv)?"