简体   繁体   English

将.txt转换为.csv文件以绘制两列时收到错误

[英]Error received while converting a .txt in to .csv file for plotting two columns

I have four columns in a .txt file and I want to convert in into .csv and then plot two of the columns. 我在.txt文件中有四列,我想转换成.csv,然后绘制其中两列。 my code is the following : 我的代码如下:

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()

I am getting an unsusal syntax error whie executing it with python N56_mass.txt csv_flash.py , the error suggests: 我在使用python N56_mass.txt csv_flash.py执行该代码时遇到了一个严重的语法错误,该错误提示:

File "N56_mass.txt", line 2
    0.1e10                     0.00326                 1.605             0.00203
                                     ^
SyntaxError: invalid syntax

I can't see the error either in my .txt file or in the code, I am uploading the .txt here too, please help me find the error. 我在.txt文件或代码中都看不到错误,我也在此处上传.txt,请帮助我找到错误。 thanks in advance.` 预先感谢。

#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

Your command line syntax is incorrect. 您的命令行语法不正确。

You are passing a text file to python instead of a valid python script by calling python N56_mass.txt csv_flash.py 您正在通过调用python N56_mass.txt csv_flash.py将文本文件传递给python而不是有效的python脚本

Try instead, 试试吧

python csv_flash.py N56_mass.txt

使用delim_whitespace将空格作为定界符处理:

dataframe = pd.read_csv("N56_mass.txt", delim_whitespace=True, usecols=columns_to_keep)

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

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