python - Add Header row to multiple .txt files using -


python newbie hoping can little bit of help. have bunch of .txt files listing gps data. create python script open each .txt file in directory , add headers (prior converting .csv , processing esri gis feature classes). have python code list .txt files in target directory , have python code add headers single specified .txt file not sure how tie 2 bits of code whole script process .txt files

python code list .txt files:

import glob  workspace = "c:\\pathway\\totarget" date = time.strftime('%y_%m_%d') directory = workspace + "\\" + date  glob.glob(directory + "./*.txt") 

so, if

import glob  workspace = "c:\\pathway\\totarget" date = time.strftime('%y_%m_%d') directory = workspace + "\\" + date  listoffiles = glob.glob(directory + "./*.txt") print listoffiles 

i list of .txt files in target directory. far good.

================

python add header specified .txt file

listoffiles = "c:\\pathway\\to\\target_dir\\log - copy (2).txt" headers = "sent_id,time_utc,nav_warn,lat,n_s,long,e_w,speed,course,date,mag_var,,check".split()  line in fileinput.input([listoffiles], inplace=true):     if fileinput.isfirstline():         print '\t'.join(headers)     print line, 

this changes first line of specfied .txt file. good.

================

amalgamated code

however if this:

import glob import fileinput import os import time     workspace = "c:\\pathway\\totarget" date = time.strftime('%y_%m_%d') directory = workspace + "\\" + date  listoffiles = glob.glob(directory + "./*.txt") headers = "sent_id,time_utc,nav_warn,lat,n_s,long,e_w,speed,course,date,mag_var,,check".split()  line in fileinput.input([listoffiles], inplace=true):     if fileinput.isfirstline():         print '\t'.join(headers)     print line, 

the error is: typeerror: can concatenate list (not "str") list

  message   file name   line    position     traceback                <module>    c:\users\pennychr\desktop\gps_2.py  58       next    c:\python27\arcgis10.2\lib\fileinput.py 253      readline    c:\python27\arcgis10.2\lib\fileinput.py 318      "typeerror: can concatenate list (not ""str"") list" 

if can give me pointers correct way me work around issue, great.

it looks there error trying generate listoffiles list using glob.glob(). following previous concatenations create directory string, i'd suggest replacing line following:

listoffiles = glob.glob(directory + "\\*.txt") 

and see if works. you've been using windows filepaths along, yet './*.txt' unix-style "all files ending in .txt in current directory." doesn't work in situation, because adds . end of directory before \\*.txt part.

when debugging, it's extremely helpful either run through code line-by-line in idle, ipython, or utility, checking variable's values @ each step. alternatively, put in print statement after every variable assignment check current value is, , ensure conforms think should be.

in addition above error, else caught eye:

headers = "sent_id,time_utc,nav_warn,lat,n_s,long,e_w,speed,course,date,mag_var,,check".split() 

does not think does. string.split() function, when not given arguments, splits on whitespace, not present in input. therefore, print '\t'.join(headers) statement later on isn't doing except converting single-item list headers string (with no tabs, btw). observe:

>>> "sent_id,time_utc,nav_warn,lat,n_s,long,e_w,speed,course,date,mag_var,,check".split() ['sent_id,time_utc,nav_warn,lat,n_s,long,e_w,speed,course,date,mag_var,,check'] 

it returns single-item list full string member. however, splitting on comma , yields:

>>> "sent_id,time_utc,nav_warn,lat,n_s,long,e_w,speed,course,date,mag_var,,check".split(',') ['sent_id', 'time_utc', 'nav_warn', 'lat', 'n_s', 'long', 'e_w', 'speed', 'course', 'date', 'mag_var', '', 'check'] 

which suspect trying for. yet reason checking variables , output.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -