0

I am trying to convert the first column of multiple (I mean like a lot!) of .txt files from wavelength (some in nanometers and some in micrometers) to wavenumber (cm^-1). Then, finally, I want to convert all the .txt files into .csv files.

Currently using: Jupyter QtConsole 5.3.2 Python 3.9.13 (main, Aug 25 2022, 18:29:29)

THIS IS MY CURRENT CODE

import os
import pandas as pd
for dirName, subdirlist, fileList in os.walk('C:\\Users\Me\Documents\1testcopy') :
>>for filename in fileList:
    >>>if fname.endswith('.txt'):
        >>>>df = pd.read_csv(fname, "sep=\t", header=None)
        >>>>df[0] = (1/df[0]) * 10000000
        >>>>df.to_csv(fname.replace('.txt', '.csv'), index=False, header=False)`

Sometimes it just doesn't respond. Other times, it says

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

But I try all the different ways to write the path. I cannot seem to figure out what is going on. I would appreciate any help on this!

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
  • 1
    Make the path string a raw string by appending an `r` in front of the opening quote: `os.walk(r'C:\Users\Me\Documents\1testcopy')`. If it's not a raw string, it tries to escape `\U`. – GordonAitchJay Feb 09 '23 at 13:12
  • I have already tried this -- and it simply doesn't do anything? If that makes sense. I write the whole code out with the 'r' at the front of the path - but then I click enter and nothing happens. Do you know why this is? – Mia Frothingham Feb 09 '23 at 13:19
  • 1
    `if fname.endswith('.txt'):` should be `if filename.endswith('.txt'):`, and `pd.read_csv(fname, "sep=\t", header=None)` needs to be `pd.read_csv(os.path.join(dirName, filename), "sep=\t", header=None)`. filename is merely the individual filename. You need to join it with the directory path (`dirName`) to get its absolute path. If you're not already familiar, I would read up on the idea of the _current working directory_ (cwd), and absolute vs relative filepaths. It's easy to get tripped up otherwise. – GordonAitchJay Feb 09 '23 at 13:35
  • Thank you, Gordon! Ok, I did this and it still does not do anything. I click enter and it doesn't say or produce anything. Here is my code: `import os import pandas as pd for dirName, subdirlist, fileList in os.walk(r'C:\Users\miabellefrothingham\Documents\1testcopy') : for filename in fileList: if filename.endswith('.txt'): df = pd.read_csv(os.path.join(dirName, filename), "sep=\t", header=None) df[0] = (1/df[0]) * 10000000 df.to_csv(fname.replace('.txt', '.csv'), index=False, header=False)'` – Mia Frothingham Feb 09 '23 at 13:39
  • 1
    No problemo. Again, replace `fname` with `filename`: `filename.replace('.txt', '.csv')` – GordonAitchJay Feb 09 '23 at 13:47
  • 1
    Note that the new csv file with be created in the current working directory. You can get the cwd with `os.getcwd()`, and change it with `os.chdir()`. – GordonAitchJay Feb 09 '23 at 13:49
  • Thank you, Gordon! You have some great patience! I am truly sorry to bother but it still does not seem to run :( `import os import pandas as pd for dirName, subdirlist, fileList in os.walk(r'C:\Users\miabellefrothingham\Documents\1testcopy') : for filename in fileList: if filename.endswith('.txt'): df = pd.read_csv(os.path.join(dirName, filename), "sep=\t", header=None) df[0] = (1/df[0]) * 10000000 df.to_csv(filename.replace('.txt', '.csv'), index=False, header=False)` – Mia Frothingham Feb 09 '23 at 13:55
  • You're welcome! `"sep=\t"` should be `sep="\t"`: `pd.read_csv(os.path.join(dirName, filename), sep="\t", header=None)` – GordonAitchJay Feb 09 '23 at 14:11
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251757/discussion-between-mia-frothingham-and-gordonaitchjay). – Mia Frothingham Feb 09 '23 at 14:20

0 Answers0