CSV 文件在通过 Python 写入后被 OS 视为 'data' 而不是 'ASCII'
CSV file seen as 'data' rather than 'ASCII' by OS after written via Python
我正在使用 Python 2.7.5 读取 CSV 文件 (input.csv),忽略某些行,并将结果写入新的 CSV 文件 (output.csv ).我做了很多不同的尝试,但它们都导致操作系统(Red Hat 和 Mac OS X)将输出文件视为 'data',而不是 'ASCII text'.
input.csv:
cat -v input.csv (truncated)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\Spooler,yes,1^M
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs,no,A^M
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager,seed,0x714b3c99^M
file input.csv
input.csv: data
script.py(最新尝试):
import io
input_file = '/Users/spork_user/Desktop/input.csv'
output_file = '/Users/spork_user/Desktop/output.csv'
with io.open(input_file, 'r', newline='\r\n') as infile, io.open(output_file, 'w', newline='\n') as outfile:
for line in infile:
#filters for lines I don't want, for example:
if "Does not exist" in line:
continue
#to verify how the line appears to python when it reads it in
print repr(line)
#without the rstrip, i get a blank line between each line in my output, and it's still seen as 'data'
outfile.write(unicode(line.rstrip('\r\n')+'\n'))
运行:
python script.py (truncated)
u'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\Spooler,yes,1\r\n'
u'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs,no,A\r\n'
u'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager,seed,0x714b3c99\r\n'
output.csv:
cat -v output.csv (truncated)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\Spooler,yes,1
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs,no,A
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager,seed,0x714b3c99
file output.csv
output.csv: data
无论我尝试使用何种组合打开 read/write 标志或去除换行符,output.csv 文件最终总是被 OS 视为 'data' .
但是,如果我制作一个带有硬编码输出的简化脚本,它会为我提供我正在寻找的 ASCII 类型的文件:
simplified.py:
import io
output_file = '/Users/spork_user/Desktop/simple_output.csv'
with io.open(output_file, 'w', newline='\n') as outfile:
outfile.write(unicode('hello\n'))
outfile.write(unicode('this\n'))
outfile.write(unicode('works\n'))
运行:
python simplified.py
<no output>
简单_output.csv:
cat -v simple_output.csv
hello
this
works
file simple_output.csv
simple_output.csv: ASCII text
如何让 OS 将 output.csv 视为像 simple_output.csv 这样的 ASCII 文本?
谢谢
你input.csv
文件是正确的。为了简化跨不同体系结构的 CSV 文件的移植,约定是行尾应该是 \r\n
,即使文本文件在 '\n'(类 Unix)或 \r
中的本地约定也是如此(Mac)
问题是文件实用程序没有意识到这一点,并且 错误地 将文件指向二进制文件,它可以说 text/csv 文件 或至少 MS/DOS 文本文件
参考:Comma-Separated Values on Wikipedia 说:
标准化
...
RFC 4180 形式化 CSV。它定义了 MIME 类型 "text/csv",遵循其规则的 CSV 文件应该具有非常广泛的可移植性。其中要求:
- 以 (CR/LF) 个字符结尾的 MS-DOS 样式行(最后一行可选)
- ...
然后怎么办:忽略 file
说文件是数据的问题,这是一个完全正确的 text/csv 文件(无论如何,像 vim 这样的好编辑可以处理行尾的不同约定)
我正在使用 Python 2.7.5 读取 CSV 文件 (input.csv),忽略某些行,并将结果写入新的 CSV 文件 (output.csv ).我做了很多不同的尝试,但它们都导致操作系统(Red Hat 和 Mac OS X)将输出文件视为 'data',而不是 'ASCII text'.
input.csv:
cat -v input.csv (truncated)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\Spooler,yes,1^M
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs,no,A^M
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager,seed,0x714b3c99^M
file input.csv
input.csv: data
script.py(最新尝试):
import io
input_file = '/Users/spork_user/Desktop/input.csv'
output_file = '/Users/spork_user/Desktop/output.csv'
with io.open(input_file, 'r', newline='\r\n') as infile, io.open(output_file, 'w', newline='\n') as outfile:
for line in infile:
#filters for lines I don't want, for example:
if "Does not exist" in line:
continue
#to verify how the line appears to python when it reads it in
print repr(line)
#without the rstrip, i get a blank line between each line in my output, and it's still seen as 'data'
outfile.write(unicode(line.rstrip('\r\n')+'\n'))
运行:
python script.py (truncated)
u'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\Spooler,yes,1\r\n'
u'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs,no,A\r\n'
u'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager,seed,0x714b3c99\r\n'
output.csv:
cat -v output.csv (truncated)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\Spooler,yes,1
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs,no,A
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager,seed,0x714b3c99
file output.csv
output.csv: data
无论我尝试使用何种组合打开 read/write 标志或去除换行符,output.csv 文件最终总是被 OS 视为 'data' .
但是,如果我制作一个带有硬编码输出的简化脚本,它会为我提供我正在寻找的 ASCII 类型的文件:
simplified.py:
import io
output_file = '/Users/spork_user/Desktop/simple_output.csv'
with io.open(output_file, 'w', newline='\n') as outfile:
outfile.write(unicode('hello\n'))
outfile.write(unicode('this\n'))
outfile.write(unicode('works\n'))
运行:
python simplified.py
<no output>
简单_output.csv:
cat -v simple_output.csv
hello
this
works
file simple_output.csv
simple_output.csv: ASCII text
如何让 OS 将 output.csv 视为像 simple_output.csv 这样的 ASCII 文本?
谢谢
你input.csv
文件是正确的。为了简化跨不同体系结构的 CSV 文件的移植,约定是行尾应该是 \r\n
,即使文本文件在 '\n'(类 Unix)或 \r
中的本地约定也是如此(Mac)
问题是文件实用程序没有意识到这一点,并且 错误地 将文件指向二进制文件,它可以说 text/csv 文件 或至少 MS/DOS 文本文件
参考:Comma-Separated Values on Wikipedia 说:
标准化
...
RFC 4180 形式化 CSV。它定义了 MIME 类型 "text/csv",遵循其规则的 CSV 文件应该具有非常广泛的可移植性。其中要求:
- 以 (CR/LF) 个字符结尾的 MS-DOS 样式行(最后一行可选)
- ...
然后怎么办:忽略 file
说文件是数据的问题,这是一个完全正确的 text/csv 文件(无论如何,像 vim 这样的好编辑可以处理行尾的不同约定)