File read/line,csv
code: test.csv
id,name
1,foo
2,bar
3,baz
This is line
%
This is second line
it is iteresting thing
%
This is third line
code: python
def open_file(filepath):
try:
return open(filepath, 'r')
except FileNotFoundError:
print(f"Error: File not found - {filepath}")
raise
except Exception as e:
print(f"Error: Unexpected error opening file - {e}")
raise
def process_file(filepath):
texts = []
current = []
try:
with open_file(filepath) as f:
f.readline() # skip header
for line in f:
items = line.split(",")
if len(items) == 2:
print(f"id = {items0.strip()} name = {items1.strip()}")
continue
if line.strip() == "%":
if current:
texts.append("".join(current))
current = []
else:
current.append(line)
if current:
texts.append("".join(current))
except Exception as e:
print(f"Error processing file: {e}")
return []
return texts
def main():
try:
texts = process_file("./test.csv")
print("\nText blocks:", texts)
except Exception as e:
print(f"Failed to process file: {e}")
if __name__ == "__main__":
main()