Copy a column from a txt file to a column in Excel file, Python -


i trying copy contents of first 3 columns of txt file first 3 columns of excel file.

here code:

import xlsxwriter worksheet1 = workbook.add_worksheet() worksheet1.write('a1', 'time', bold);worksheet1.write('b1', 'user value', bold);worksheet1.write('c1','address', bold);worksheet1.write('d1', 'serial number', bold); items = os.listdir(directory) file in items:     if file.endswith('file.txt'):             fileselection = directory+'/' + file             array1 = []             open(fileselection, 'r') f:                     line in f:                         valueslist = line.split('\t')                         #print valueslist                         array1.append(valueslist)          j in range(len(array1)):             if j == 0:                 continue             else:                 print array1[j][0]                 worksheet1.write('a2:d2', array1[j][0]) #i want say, copy columns d start second raw 

however copies whole txt array first column of excel file!

it looks 'text file' want csv, different delimiter (this bit of confusing convention). can use csv module specify this:

>>> import csv >>> open('your file.txt', 'rb') csvfile: ...     reader = csv.reader(csvfile, delimiter='\t') ...     row in reader: ...         print ', '.join(row) 

there similar csv writer module, or want excel file?

i have noticed in code maybe valueslist should truncate first 3 columns, appear want...

this why getting of original file. change

 valueslist = line.split('\t') 

to

valueslist = line.split('\t')[:3] 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -