How to convert an excel fiile to .dat file using OpenXML - openxml

How can we convert excel file to *.dat file? What xl format I should use while conversion?

.dat files are just .txt files delimited with " | ".
Use openXML and just write the data to a text file using a filewriter, saving as .dat

Related

Append files and add column with last part of each filename

I hope one of you are willing to help a complete Python beginner.
I have managed to create my first script where I append multiple excel files in a folder into one merged file. So far so good!
But I also need the script to create an additional column and complete it with the last two characters of the filename from each file it appends.
My script looks like this for now:
import pandas as pd
import glob
# getting excel files to be merged from the Desktop
path = "C:\\Users\\123\\OneDrive\\Descriptions\\Translated"
# read all the files with extension .xlsx i.e. excel
filenames = glob.glob(path + "\*.xlsx")
print('File names:', filenames)
# empty data frame for the new output excel file with the merged excel files
outputxlsx = pd.DataFrame()
# for loop to iterate all excel files
for file in filenames:
# using concat for excel files
# after reading them with read_excel()
df = pd.concat(pd.read_excel(file, sheet_name=None), ignore_index=True, sort=False)
# appending data of excel files
outputxlsx = outputxlsx.append( df, ignore_index=True)
print('Final Excel sheet now generated at the same location:')
outputxlsx.to_excel("C:/Users/123/OneDrive/Descriptions/Translated/Merged.xlsx", index=False)
The files in the folder are named like this:
CZ, PL, TR_cs-CZ
CZ, PL, TR_pl-PL
CZ, PL, TR_tr-TR
So the last column should be:
CZ
PL
TR
Thank you!!

Python Encoding String Issue: String "Île-de-France" is being written to CSV file as "√éle-de-France"

I'm trying to write a string "Île-de-France" to a CSV file, however, what is being written to file is "√éle-de-France". I tried encoding="utf-8" in the open and it didn't work. What am I missing? Thanks!
with open('/tmp/directwrite.csv', 'w', newline='') as csvfile:
fieldnames = ['location']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'location': 'Île-de-France'})
It’s an issue with your viewer. If using Excel or some other windows program, useencoding='utf-8-sig'. That writes a signature at the beginning of the file that Windows programs recognize as a UTF-8 file; otherwise, it assumes an ANSI encoding for the file.

Save files into different format using original filenames

After reading .csv files in a directory, I want to save each of them into .html files using their original file names. But, my code below brings along the extension (.csv) from the original filenames.
For example,
Original files: File1.csv, File2.csv
Result files: File1.csv.html, File2.csv.html
I want to remove ".csv" from the new file names.
import pandas as pd
import glob, os
os.chdir(r"C:\Users\.....\...")
for counter, file in enumerate(glob.glob("*.csv")):
df = pd.read_csv(file, skipinitialspace=False, sep =',', engine='python')
df.to_file(os.path.basename(file) + ".html")
The code below removed ".csv" but also ".html"
df.to_file(os.path.basename(file + ".html").split('.')[0])
My expectation is:
File1.html, File2.html
EDIT:
Another post [How to get the filename without the extension from a path in Python? suggested how to list existing files without extensions. My issue, however, is to read existing files in a directory and save them using their original file names (excluding original extension) with new extension.

Convert csv file to mat file

I have a csv file as follows:
StringA,StringB ...
1.234,13.45 ...
I want to convert this into a .mat file extension but when I try to read the csv file, it throws and error due to the fact that I have strings in the first row. This makes it difficult to directly convert to a mat file as I also want the headers in my mat file. How would I go about doing this?
Given your file format, you can use importdata() like this:
M = importdata('myfile.txt', ',', 1);
The header will be saved in M.colheaders, the data will be stored in M.data.
Please have a look at the documentation for further information and a working example.

How can I convert an Excel file to CSV with Perl's Spreadsheet::WriteExcel?

I have an Excel sheet and am able to convert it into .csv format using Perl. My only problem is that some of my data in the Excel sheet cells contain commas and that
has to be retained in the CSV format also, but while converting it takes as a
seperator. How can I retreive the data in the cell with commas as it is and print it in
the CSV?
For example, in the Excel sheet A1 cell contains the data {0xAAAA,0xFFFF,0xAAAA,0xAAAA} I want the same data with commas in the A1 cell in CSV file also. I am Spreadsheet::ParseExcel.
Use Text::CSV for parsing and creating your csv files.
Be more specific if you do not know how to use the module for your task.
To avoid the commas in your fields to be interpreted as separators, enclose your fields with characters like double quotes:
"10","10,00","156"