i m trying to extract a specific column from a arabic file to another file this is my code
# coding=utf-8
import csv
from os import open
file = open('jamid.csv', 'r', encoding='utf-8')
test = csv.reader(file)
f = open('col.txt','w+', 'wb' ,encoding='utf-8')
for row in test:
if len(row[0].split("\t"))>3 :
f.write((row[0].split("\t"))[3].encode("utf-8"))
f.close()
and the file is like this :
4 جَوَارِيفُ جواريف جرف اسم
18 حَرْقى حرقى حرق اسم
24 غَزَواتٌ غزوات غزو اِسْمٌ
i keep gitting the same error :
File "col.py", line 5, in <module> file = open('jamid.csv', 'r', encoding='utf-8')
TypeError: an integer is required (got type str)
I see a couple of problems with your code. First, you are using the signature of the open
function with os.open
, but it has different paramters. You can stick with open
. More importantly, you seem to be trying to fix the row coming out of csv.reader
by splitting it again on tabs.
My guess is that you saw the entire line in row[0]
so tried to fix it. But the problem is that that the reader splits on commas by default - you need to supply a different delimiter. Here its a bit problematic because your code splits with a tab but the example shows spaces. I used spaces in my solution, but you can switch that as needed.
Finally, you attempted to encode the strings before giving them to the output file object. That object should be opened with the right encoding and you should simply give it strings.
# coding=utf-8
import csv
with open('jamid.csv', 'r', newline='', encoding='utf-8') as in_fp:
with open('col.txt','w', newline='', encoding='utf-8') as out_fp:
csv.writer(out_fp).writerows(row[3] for row in
csv.reader(in_fp, delimiter=' ', skipinitialspace=True)
if len(row) >= 3)