保留文本列中的换行符并将其转换为 CSV 时。

huangapple go评论73阅读模式
英文:

Keep enter space in text column when converting to csv

问题

以下是要翻译的内容:

I have data in excel where is text with enter space in the last column. Here is examples of my data:

If I convert using python to csv, my data looks like this:

I need the TEXT column will be like this:

This is my script:

import pandas as pd
import os
import numpy as np

WD = r'XXX'
os.chdir(WD) 

for file in os.listdir(WD):
    if file.endswith('.xlsx'):
        FILE = file

        sheet_names = pd.ExcelFile(FILE).sheet_names  

        for sn in sheet_names:
            OUTPUT_FILE = '{}_{}'.format(sn, FILE.replace('.xlsx', '.csv'))
            df = pd.read_excel(FILE)
            print(FILE, sn)

            for col in df.columns.to_list():
                df[col] = df[col].map({True: '', False: ''}).fillna(df[col])

                cn = ['IN', 'NAME', 'TEXT']
                df = df.reindex(columns=cn)

                df.to_csv(OUTPUT_FILE, sep='|', encoding='utf-8-sig', index=False)

Do you have any idea?

英文:

I have data in excel where is text with enter space in last column. Here is examples of my data:

保留文本列中的换行符并将其转换为 CSV 时。

If I convert using python to csv, my data looks like this:

保留文本列中的换行符并将其转换为 CSV 时。

I need the TEXT column will be like this:

保留文本列中的换行符并将其转换为 CSV 时。

This is my script:

import pandas as pd
import os
import numpy as np


WD = r'XXX'
os.chdir(WD) 

for file in os.listdir(WD):
    if file.endswith('.xlsx'):
        FILE = file

        sheet_names = pd.ExcelFile(FILE).sheet_names  

        for sn in sheet_names:
            OUTPUT_FILE = '{}_{}'.format(sn,FILE.replace('.xlsx','.csv'))
            df = pd.read_excel(FILE,)
            print(FILE, sn)

            for col in df.columns.to_list():
                df[col] = df[col].map({True: '', False: ''}).fillna(df[col])


                cn = ['IN', 'NAME', 'TEXT']
                df = df.reindex(columns = cn)

                df.to_csv(OUTPUT_FILE,sep='|',encoding='utf-8-sig',index=False)

Do you have any idea?

答案1

得分: 1

以下是您要翻译的内容:

Excel to csv:

import pandas as pd
df = pd.read_excel('./keep_enter.xlsx')
def replace_custom_func(x):
    new_str = ''
    if len(x) > 0:
        for i in x.split('\n'):
            new_str += f'"{i}"&CHAR(10)&'
        return "=" + new_str[:-10]
    else:
        return x
df['Text'] = df['Text'].apply(lambda x: replace_custom_func(x))
df.to_csv('keep_enter1.csv', sep='|', index=False)

CSV to Excel:

df = pd.read_csv('./keep_enter1.csv', sep='|')
writer = pd.ExcelWriter('new_excel_replace12345.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1', index=False)

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']
format = workbook.add_format({'text_wrap': True})
worksheet.set_column('C:D', None, format)
worksheet.write_formula(1, 2, df['Text'][0])
# Close the Pandas Excel writer and output the Excel file.
writer.save()

Output:
保留文本列中的换行符并将其转换为 CSV 时。

英文:

I hope this works for your solution, (pip install xlsxwriter) before executing

Excel to csv:

import pandas as pd
df = pd.read_excel('./keep_enter.xlsx')
def replace_custom_func(x):
    new_str = ''
    if len(x) > 0:
        for i in x.split('\n'):
            new_str += f'"{i}"&CHAR(10)&'
        return "=" + new_str[:-10]
    else:
        return x
df['Text'] = df['Text'].apply(lambda x: replace_custom_func(x))
df.to_csv('keep_enter1.csv', sep='|', index=False)

CSV to Excel:

df = pd.read_csv('./keep_enter1.csv', sep='|')
writer = pd.ExcelWriter('new_excel_replace12345.xlsx', engine='xlsxwriter')

# # Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1', index=False)

# # Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']
format = workbook.add_format({'text_wrap': True})
worksheet.set_column('C:D', None, format)
worksheet.write_formula(1, 2, df['Text'][0])
# # Close the Pandas Excel writer and output the Excel file.
writer.save()

Output:
保留文本列中的换行符并将其转换为 CSV 时。

huangapple
  • 本文由 发表于 2023年2月8日 19:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定