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

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

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:

  1. import pandas as pd
  2. import os
  3. import numpy as np
  4. WD = r'XXX'
  5. os.chdir(WD)
  6. for file in os.listdir(WD):
  7. if file.endswith('.xlsx'):
  8. FILE = file
  9. sheet_names = pd.ExcelFile(FILE).sheet_names
  10. for sn in sheet_names:
  11. OUTPUT_FILE = '{}_{}'.format(sn, FILE.replace('.xlsx', '.csv'))
  12. df = pd.read_excel(FILE)
  13. print(FILE, sn)
  14. for col in df.columns.to_list():
  15. df[col] = df[col].map({True: '', False: ''}).fillna(df[col])
  16. cn = ['IN', 'NAME', 'TEXT']
  17. df = df.reindex(columns=cn)
  18. 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:

  1. import pandas as pd
  2. import os
  3. import numpy as np
  4. WD = r'XXX'
  5. os.chdir(WD)
  6. for file in os.listdir(WD):
  7. if file.endswith('.xlsx'):
  8. FILE = file
  9. sheet_names = pd.ExcelFile(FILE).sheet_names
  10. for sn in sheet_names:
  11. OUTPUT_FILE = '{}_{}'.format(sn,FILE.replace('.xlsx','.csv'))
  12. df = pd.read_excel(FILE,)
  13. print(FILE, sn)
  14. for col in df.columns.to_list():
  15. df[col] = df[col].map({True: '', False: ''}).fillna(df[col])
  16. cn = ['IN', 'NAME', 'TEXT']
  17. df = df.reindex(columns = cn)
  18. df.to_csv(OUTPUT_FILE,sep='|',encoding='utf-8-sig',index=False)

Do you have any idea?

答案1

得分: 1

以下是您要翻译的内容:

Excel to csv:

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

CSV to Excel:

  1. df = pd.read_csv('./keep_enter1.csv', sep='|')
  2. writer = pd.ExcelWriter('new_excel_replace12345.xlsx', engine='xlsxwriter')
  3. # Convert the dataframe to an XlsxWriter Excel object.
  4. df.to_excel(writer, sheet_name='Sheet1', index=False)
  5. # Get the xlsxwriter workbook and worksheet objects.
  6. workbook = writer.book
  7. worksheet = writer.sheets['Sheet1']
  8. format = workbook.add_format({'text_wrap': True})
  9. worksheet.set_column('C:D', None, format)
  10. worksheet.write_formula(1, 2, df['Text'][0])
  11. # Close the Pandas Excel writer and output the Excel file.
  12. writer.save()

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

英文:

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

Excel to csv:

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

CSV to Excel:

  1. df = pd.read_csv('./keep_enter1.csv', sep='|')
  2. writer = pd.ExcelWriter('new_excel_replace12345.xlsx', engine='xlsxwriter')
  3. # # Convert the dataframe to an XlsxWriter Excel object.
  4. df.to_excel(writer, sheet_name='Sheet1', index=False)
  5. # # Get the xlsxwriter workbook and worksheet objects.
  6. workbook = writer.book
  7. worksheet = writer.sheets['Sheet1']
  8. format = workbook.add_format({'text_wrap': True})
  9. worksheet.set_column('C:D', None, format)
  10. worksheet.write_formula(1, 2, df['Text'][0])
  11. # # Close the Pandas Excel writer and output the Excel file.
  12. 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:

确定