简体   繁体   English

Pandas DataFrame输出为ASCII ^ A或Unicode SOH分隔文件

[英]Pandas DataFrame output as ASCII ^A or Unicode SOH delimited file

Finished my processing of a DataFrame and now I want to export it. 完成了对DataFrame的处理,现在我想将其导出。 Normally I export it as a plain CSV and there isn't a problem. 通常,我将其导出为纯CSV文件,没有问题。 Today I am asked to export the file with a ASCII ^A or the equivalent Unicode SOH delimiter. 今天,我被要求使用ASCII ^ A或等效的Unicode SOH分隔符导出文件。 I will be frank this is the first time I have ever had to deal with ASCII/Unicode delimiters. 坦率地说,这是我第一次不得不处理ASCII / Unicode分隔符。 Can you guys offer me some guidance/help as to how to go about this in pandas/python? 你们能为我提供一些有关如何在pandas / python中进行操作的指导/帮助吗? I would really appreciate it. 我真的很感激。 Thanks in advance for your help. 在此先感谢您的帮助。

In Python you can convert any number to a character using chr() . 在Python中,您可以使用chr()将任何数字转换为chr() For your example I think you want chr(1) which is ASCII SOH. 对于您的示例,我认为您想要的是chr(1) ,它是ASCII SOH。 You can then pass this as the separator: 然后,您可以将其作为分隔符传递:

df.to_csv('foo.txt', sep=chr(1))

Once you see what chr(1) produces ( \\x01 ), you may realize you can also write it this way: 一旦看到了chr(1)产生了什么( \\x01 ),您可能会意识到也可以这样写:

df.to_csv('foo.txt', sep='\1')

The ASCII table, of course, is here: http://www.asciitable.com/ ASCII表,当然在这里: http : //www.asciitable.com/

^A is ASCII SOH according to this: http://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ctrl.htm - there is no Python escape code for it though. ^ A根据此是ASCII SOH: http : //academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ctrl.htm-尽管没有Python逸出代码。 curses.ascii.SOH is close, but it's actually just the constant 1 so pretty useless - you'd be better off just writing SOH = '\\1' in your program. curses.ascii.SOH接近,但实际上只是常数1因此非常没用-您最好在程序中编写SOH = '\\1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM