我在.txt文件中有四列,我想将其转换为.csv,然后绘制两列。我的代码如下:
import pandas as pd
import numpy as np
from sys import argv
from pylab import *
import csv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
# read flash.dat to a list of lists
datContent = [i.strip().split() for i in open("./N56_mass.txt").readlines()]
# write it as a new CSV file
with open("./N56_mass.txt", "wb") as f:
writer = csv.writer(f)
writer.writerows(datContent)
columns_to_keep = ['#Radius(cm)', 'Ni56MassFration']
dataframe = pd.read_csv("./N56_mass.txt", usecols=columns_to_keep)
pd.set_option('display.height', 1000)
pd.set_option('display.max_rows', 1000)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
dataframe.plot(x='#Radius(cm)', y='Ni56MassFraction', style='r')
print dataframe
show()
我在使用python N56_mass.txt csv_flash.py
执行时出现了一个不正确的语法错误,错误提示:
File "N56_mass.txt", line 2
0.1e10 0.00326 1.605 0.00203
^
SyntaxError: invalid syntax
我无法在我的.txt文件或代码中看到错误,我也在这里上传.txt,请帮我找到错误。提前谢谢。
#Radius(cm) Ni56Mass TotalMass Ni56MassFraction
0.1e10 0.00326 1.605 0.00203
0.2e10 0.00548 1.896 0.00289
0.3e10 0.00620 1.976 0.00313
0.4e10 0.00658 2.028 0.00324
0.6e10 0.00716 2.113 0.00339
0.8e10 0.00777 2.177 0.00357
1.0e10 0.00808 2.218 0.00364
1.2e10 0.00833 2.247 0.00370
1.4e10 0.00851 2.268 0.00357
1.6e10 0.00859 2.281 0.00377
1.8e10 0.00862 2.288 0.00377
2.0e10 0.00864 2.293 0.00377
2.2e10 0.00864 2.297 0.00377
2.4e10 0.00864 2.299 0.00376
2.6e10 0.00865 2.301 0.00376
答案 0 :(得分:1)
您的命令行语法不正确。
通过调用python N56_mass.txt csv_flash.py
尝试改为,
python csv_flash.py N56_mass.txt
答案 1 :(得分:1)
使用delim_whitespace
将空格作为分隔符处理:
dataframe = pd.read_csv("N56_mass.txt", delim_whitespace=True, usecols=columns_to_keep)