I have a script that opens a csv file, applies a substitution and then writes out to a new file then deletes the old one.  with open('file-presub.csv', 'r') as reader, open('file-postsub.csv','w') as writer:     for row in reader:         row = re.sub('(\+*,[^,]*,[^,]*,[^,]*,\ 0)', ' (Core)\\g&lt;1&gt;', row)         writer.write(row)  os.remove("file-presub.csv")   Is there any way I can make it so I just edit the file-presub.csv and write it out with the same filename instead of making a second file and deleting the original? I'd like to avoid importing OS just for this one line.  Of course if it's not possible I'm able to import OS, just asking if it's possible at all.
64135
I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?
56058
I've written a python script using arcpy which outputs a polygon feature class into a File Geodatabase.  I have added a function to export the attributes to a separate CSV file.  I am using the code which I have found in  which works perfectly.  However, that code exports every column in the feature class.  I only want to export the fields that do not have the following names: OBJECTID, Shape, or Shape_Length.  My CSV file generates successfully, and it correctly does not include the OBJECTID or Shape_Length fields.  However, the Shape field is written to the file.  An example value that is written to that field is:  &lt;geoprocessing describe geometry object object at 0x28CB90A0&gt;   I have added a line to print the field names as it iterates through them and surprisingly, Shape is not printed.  It's as if ArcGIS is hiding it or gives it a different name.  The code for my function is below:  def exportToTable():     """          Exports the final outputs to a CSV File.     """      # Create path to CSV File (note the varialbe outputPath is declared elsewhere).     CSVFile = outputPath+'\\FinalOutput.csv'     arcpy.AddMessage("Created CSV File: %s" %CSVFile)      # Get all fields in FinalOutput feature class and remove unwanted fields.     fields = arcpy.ListFields('FinalOutput')     for field in fields:         arcpy.AddMessage("Field.name is:"+field.name) #not printing 'Shape' field name         if field.name in (['OBJECTID', 'Shape', 'Shape_Length']):             fields.remove(field)      i = 1     f=open(CSVFile, 'w')     for field in fields:         #--write the wanted field names to the output file         if i &lt; len(fields):             f.write('%s,' % field.name)             i += 1         else:             f.write('%s\n' % field.name)      # Use a search cursor to iterate through the rows of the table and write them to the CSV file.     rows = arcpy.SearchCursor('FinalOutput')     for row in rows:         i = 1         for field in fields:             if i &lt; len(fields):                 f.write('%s,' % row.getValue(field.name))                 i += 1             else:                 f.write('%s\n' % row.getValue(field.name))     del rows     f.close()   Does anyone know what's going on here?    I modified my code to follow the advice of @sgrieve and it was still writing the Shape field.  If I add a line to print the field names as it iterates through them, it lists all the fields except the Shape field, yet it still writes to the CSV.  It also added the X and Y coordinates of the polygon as two new columns and the columns no longer aligned with the column names.  I've modified the line where @sgrieve declares the fields to be the following:   fields = [f.name for f in arcpy.ListFields('FinalCadastre') if f.type &lt;&gt; 'Geometry']   The new code works fine, but I'm still unsure what the problem was.  Does anyone know what was going on?  What's the deal with the Shape field?
100065
I have a directory with several hundred csv files whose filenames start with two digits {01..84}. Several hundred >> 84, so obviously some filenames start with the same prefix. I wish to concatenate the files whose filenames start with the same prefix. Here's what I've got:  #!/bin/bash for i in {01..84}; do         #declare array to store files with same prefix         declare -a files=()         echo "Processing $i"         for j in `ls $i*.csv`; do                 #add files with same prefix to array                 files=("${files[@]}" "$j")         done             #cat first file including header with the rest of the files without the headers          cat &lt; ${files[@]:0:1} &lt;(tail -n+2 ${files[@]:1}) &gt; "$i".csv done    So far so good ... only, it grinds to a halt at $i=22 halfway through (repeatable error), and pollutes the output files with blank lines and headers like "==> 19XXX.csv &lt;==" (without quotes).   What should I change in the code to just get a nice clean csv file for each prefix without the script crashing? Are there any precompiled bash utilities that I can call to do any of this quicker and easier?
18451
I have a 2 files with the following:   File1.txt  A 1 B 2 C 5 Z 3  File2.txt A 4 B 7  C 10 D 11   What I would like to do is create something like   A 1 4  B 2 7 C 5 10 D - 11 Z 3 -   Is there a utility that does this? If not how Can this be done? Using a find and awk or something?
6452
I have a CSV file, in.csv, which has an integer array column:  1,2,4,"{100,200,300}",,1 1,2,4,"{100,200,300,400,500}",,2 1,2,4,"{100,200,300,600.900,1200,1500}",1,3   I want to have an output file, out.csv, like:  1,2,4,,1 1,2,4,,2 1,2,4,1,3   I tried:  cut -d , -f4 -- complement in.csv &gt; out.csv   But it did not work out.  (I also have the fifth column which has value for third row but not for others).
1351
Can anyone recommend a simple API that will allow me to use read a CSV input file, do some simple transformations, and then write it.  A quick google has found  which looks promising.  I just wanted to check what others are using before I couple myself to this API.
26448
I'm having trouble parsing through a CSV file to add users.  I wrote a small script to add a list of 1000 users:  export IFS=","  cat users.csv | while read a b c d e f g; do useradd -d -u $a -U $d ; done   The CSV file is formatted as:  ps2946,male,Phil Harris,Recording,6508 Hollywood Blvd,Nov 16 1950,760 901 3537 mc3225,male,Mel Blanc,Radio,6385 Hollywood Blvd.,Oct 1 1983,626 647 5560 fn1029,male,Franklin Pangborn,Motion pictures,1500 Vine Street,Mar 10 1911,858 497 6108 we9602,undefined,Webb Pierce,Recording,1600 Vine Street,Sep 3 1988,559 595 6063   However upon running the script it iterates through the entire file repeating:  useradd: invalid home directory '-u'   I used export IFS to separate the words by comma and use them as the parameters for the useradd command.   I read the manual and used the value for the home directory, but I feel like my entire script is off.  Can someone please help me in properly formatting a script to add users from a CSV file?  For reference I used this thread as a base:
100086
What's the difference between file and open in Python?  When should I use which one?  (Say I'm in 2.5)
48423
I want to create multiple files at a time using names from a file list.txt, how can I do it?  Sample list.txt:  EOG090W002U EOG090W00C1 EOG090W00DC EOG090W00DE EOG090W00E5 EOG090W00HR EOG090W00MH EOG090W00MS EOG090W00PB EOG090W00U4 EOG090W00UK EOG090W00WM EOG090W00WR   Suppose I have this list.txt containing some id-numbers. Now I want to make separate files using these ids as the name (e.g. EOG090W002U_M0.ctl, EOG090W00C1_M0.ctl, EOG090W00DC_M0.ctl). Also, the contents of the files need to be change accordingly. For example the content of EOG090W002U_M0.ctl, EOG090W00C1_M0.ctl file will be   seqfile = EOG090W002U_p.phy treefile = Constant.txt outfile = EOG090W002U_M0_mlc   or  seqfile = EOG090W00C1_p.phy treefile = Constant.txt outfile = EOG090W00C1_M0_mlc   where *.phy and Constant.txt will be provided in the same folder.
17800
I have a file with multiple instances of Text_1 and Text1 and I need to replace both those strings with Text_A and TextB respectively.    Currently I'm doing two Find and Replace functions on each file one that finds Text_1 and replaces it with Text_A and the other that finds Text1 and replaces it with TextB.    Is there any way to do this all at once instead of having to run "Find and Replace" twice?  I am using Dreamweaver CS3, but I also have Notepad++, regular Notepad, OO Writer, MS Word if those will be easier.  Ideally I could do this in Dreamweaver or Notepad++ but I'm open to downloading something else to get the job done.  I'd prefer not to have to do any command line stuff or create a batch file (while I'm aware of it, I don't understand it really).  In case the above description isn't clear, let me explain it this way...  I want to run Find &amp; Replace 1 time in 1 document and I want it to do ALL of the following during that one Find &amp; Replace instance:   Find Text_1 and Replace with Text_A Find Text1 and Replace with TextB   I am not trying to do a Find and Replace across several documents.
70196
What is a good way to do string replacements in a file using a dictionary with a lot of substituend-substituent pairs? And by a lot, I actually mean about 20 – not much, but many enough so that I want to organize them neatly.  I kind of want to collect all substituend-substituent pairs in a file dictionary.txt in an easy-to-manage way, since I need to replace a lot of stuff, say like:  "yes"      : "no" "stop"     : "go, go, go!" "wee-ooo"  : "ooooh nooo!" "gooodbye" : "hello"  "high"     : "low" "why?"     : "i don't know"   Now I want to apply these substitutions in some file novel.txt.  Then I want to run magiccommand --magicflags dictionary.txt novel.txt so that all instances of yes in novel.txt are replaced by no (so even Bayesian would be replaced by Banoian) and all instances of goodbye in novel.txt would be replaced by hello and so forth.  So far, the strings I need to replace (and replace with) do not have any quotes (neither single nor double) in them. (It would be nice, though, to see a solution working well with strings containing quotes, of course.)  I know sed and awk / gawk can do such stuff principally, but can they also work with such dictionary files? Seems like gawk would be the right candidate for magiccommand, what are the right magicflags? How do I need to format my dictionary.txt?
69828
This is my file1:    #$BQ #{ VOL       @home    } #database    daba #relation    tcdeatid #copy           1 #{ version        0 } #opendb #clear #.lruno := 72 #.infno := 1 #.tid.noel := 101 #.tid.info := 64 #.tid.setnr := 1225              &lt;--- (1225 number will be changed) #.typeidm := 1 #.sourcetable := 2 #writedb      #clear #.lruno := 72 #.infno := 205 #.tid.noel := 101 #.tid.info := 76 #.tid.setnr := 5625              &lt;--- (5625 number will be changed) #.typeidm := 1 #.sourcetable := 2 #writedb #$EOJ  In file2, I have the correct entries:    #.tid.info := 3345              &lt;--- (I want to put this number in file1 in place of 64) #.tid.setnr := 1254              &lt;--- (I want to put this number in file1 in place of 1225) #.tid.info := 5567              &lt;--- (I want to put this number in file1 in place of 76) #.tid.setnr := 9056              &lt;--- (I want to put this number in file1 in place of 5625)   I want to make changes in file1, directed by data from file2.  I have tried sed "s/tid.setnr := 1225/tid.setnr := 1254/g" file1 &gt; modified_file1  But instead of doing it manually, I want my script to read the new tid.info and tid.setnr values from file2 and modify the respective lines in file1.  The first line in file2 should replace the entire first line in file1 that contains tid.info, the second line in file2 should replace the second line in file1 that contains tid.setnr, and so on.
37884
I have hundreds of excel files which are all of the same format (i.e. 4 worksheets per Excel file). I need to combine all the files into 1 all singing and dancing file which must have the same format as the originals (i.e. maintain the four separate worksheets, which are all identically named).  While each file is structured the same, the number of columns (and the heading names) between sheet 1 and 2 (for example) is different. So it can’t be combined into one file with everything in one sheet!  There are two complications:   I need to create an EXTRA column in the merged file (on EACH sheet) to identify the source file (“file name”). The files contain a lot of zero data entries (e.g. 55 rows of useful data followed by hundreds of rows of zeros) which I need to remove from the merged file.   I’ve never used VBA, but everyone has to start somewhere I suppose.
54555
Consider the following CSV file:  jdbc.driverClassName,oracle.jdbc.driver.OracleDriver  jdbc.username,kshitiz   It has to be transformed into:  -Djdbc.driverClassName=oracle.jdbc.driver.OracleDriver \  -Djdbc.username=kshitiz \   Given the following bash snippet:  while read l; do     IFS=',' read -ra arr &lt;&lt;&lt; "$l"     echo '-D'${arr[0]}'='${arr[1]}' \' done &lt;properties.txt   Bash prints:  \jdbc.driverClassName=oracle.jdbc.driver.OracleDriver   \jdbc.username=kshitiz   What's going on here?
94844
I have a file1.txt  USA Joe 123.123.123 Russia Marry 458.786.892 Canada Greg 151.844.165 Latvia Grace 125.895.688   and file2.txt  1 123.123.123 2 151.844.165 3 465.879.515   and I want to create a new file result.txt where I print my only those lines that adresses (xxx.xxx.xxx) are both  in file1 and file2 so my result should be  USA Joe 123.123.123 Canada Greg 151.844.165   I need to use awk, but how I need to use it for both files?
18786
I need to lock a file for writing in Python. It will be accessed from multiple Python processes at once. I have found some solutions online, but most fail for my purposes as they are often only Unix based or Windows based.
57944
I am reading a csv file in bash script as follows:  resource='/data/bscs/' while IFS='|'read seqid fname fpath  do     echo "FILENO:         $seqid"     echo "FILENAME:       $fname"     echo "FILE_PATH:    $fpath"                     done &lt; "report.csv"   My csv file has following values :  3|sample1.txt|$resource/operation/  I want the $resource to expand inside $fname. Instead, I am getting this output:  FILENO:         3 FILENAME:       sample1.txt SOURCE PATH:    $resource/operation   I have tried the following:  "${fpath}"   $(echo $fpath)  How can I achieve this?