[英]Select column from tab delimited text file in python and add it to tsv file
[英]Comma and tab delimited tsv file
這個問題是使用pandas庫的python 2.7的。 我下載了此文件: http : //language.media.mit.edu/data/public/wikipedia_userlang_iso639-3.zip這是一個帶有制表符和逗號的文件。 我已經在整個互聯網上搜索了此內容。
我想用熊貓分開。 這給出了錯誤:
df = pd.read_table('wikipedia_userlang_iso639-3.tsv', sep= '\t')
print df [:10]
因為該文件也有逗號。
非常感謝幫助!
該文件不能解析為CSV文件,因為每一行沒有固定數量的字段(范圍為2到241)。 您必須自己解析它,並決定如何為每個用戶處理可變數量的語言:
import codecs
with codecs.open('wikipedia_userlang_iso639-3.tsv', 'r', 'utf-8') as handle:
for line in handle:
chunks = line.strip().split('\t')
username = chunks[0]
languages = [c.split(',') for c in chunks[1:]]
# Do something with the above variables
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.