简体   繁体   中英

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. 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:

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. 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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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