C# Python 读取txt中的文件,将16进制的数据按照小端方式排列,处理为有符号的数字,并写入text文件;控制台和界面两种方式
2018-03-31 17:03
1196 查看
以前都是处理bin文件,突然处理text文件,不专业,方法不适用
代码如图:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
namespace HandleData
{
class Program
{
static void Main(string[] args)
{
string line = string.Empty;
HandleData.Program.ReadData("E:\\language\\python\\file\\data.txt", "E:\\language\\python\\file\\dataResult.txt");
Console.ReadLine();
}
static void ReadData(string filepath,string filepathw)
{
string strData = File.ReadAllText(filepath);
FileStream fs = File.Open(filepathw, FileMode.Create);
string[] str = strData.Split(' ','\r','\n');
StreamWriter wr = new StreamWriter(fs);
for (int k=0;k<str.Count()-2;k+=2)
{
string strA = str[k] +" "+ str[k+1];
int low;
//
if (str[k] == "")
{
k++;
}
low = Convert.ToInt16(str[k], 16);
if (str[k + 1] == "")
{
k++;
}
int high = Convert.ToInt16(str[k + 1], 16);
byte[] bytearr= { (byte)low, (byte)high };
int result = BitConverter.ToInt16(bytearr,0);
wr.WriteLine(result);
//byte[] byteArray = System.Text.Encoding.Default.GetBytes(strA);
}
wr.Flush();
wr.Close();
// 关闭文件
fs.Close();
Console.WriteLine(strData);
}
}
}
界面:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace HandleTxtData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
ofd.ValidateNames = true;
ofd.CheckPathExists = true;
ofd.CheckFileExists = true;
string strFileName="";
if (ofd.ShowDialog() == DialogResult.OK)
{
strFileName = ofd.FileName;
}
textBox1.Text = strFileName;
string path = Path.GetDirectoryName(strFileName);
string fileName = Path.GetFileNameWithoutExtension(strFileName);
string strFileNameW = path +"\\"+fileName+ "result.txt";
ReadData(strFileName,strFileNameW);
}
public void ReadData(string filepath, string filepathw)
{
string strData = File.ReadAllText(filepath);
FileStream fs = File.Open(filepathw, FileMode.OpenOrCreate);
string[] str = strData.Split(' ', '\r', '\n');
StreamWriter wr = new StreamWriter(fs);
richTextBox1.Text = "开始处理数据\n";
for (int k = 0; k < str.Count() - 2; k += 2)
{
string strA = str[k] + " " + str[k + 1];
int low;
//
if (str[k] == "")
{
k++;
}
low = Convert.ToInt16(str[k], 16);
if (str[k + 1] == "")
{
k++;
}
int high = Convert.ToInt16(str[k + 1], 16);
byte[] bytearr = { (byte)low, (byte)high };
int result = BitConverter.ToInt16(bytearr, 0);
wr.WriteLine(result);
//byte[] byteArray = System.Text.Encoding.Default.GetBytes(strA);
richTextBox1.Text = ".";
}
richTextBox1.Text = "\n数据处理完成";
wr.Flush();
wr.Close();
// 关闭文件
fs.Close();
Console.WriteLine(strData);
}
}
}
用python语言试一下,发现处理数据更快,代码如下:
#encoding=utf-8
import re
file_open=open('data.txt')
file_write=open('handle_data.txt','w')
listfile=file_open.read()
listf=re.split('[ \n\r]',listfile)
i=0
while i<len(listf)-2:
intlow=int(listf[i],16)
inthigh=int(listf[i+1],16)
i=i+2
num2=intlow+(inthigh<<8)
if (inthigh>>7)&0x01==0x01:
num2-=65536
file_write.write(str(num2));
file_write.write("\n");
#listf中的数据类型为字符串,需要转为16进制对应的数据
file_open.close()
file_write.close()
print "数据处理完成"
将.py修改为可以添加参数的脚本,在cmd路径在,输入python,拖入.py文件,再拖入要处理的data.txt,会在同一路径下生成Result.txt结果文件.代码如下#encoding=utf-8
import re
import os
import sys
file_open=open(sys.argv[1])#处理输入的文件参数
filewritepath=os.path.dirname(sys.argv[1])+"\\Result.txt"
file_write=open(filewritepath,'w')
#file_open=open('data.txt')
#file_write=open('handle_data.txt','w')
listfile=file_open.read()
listf=re.split('[ \n\r]',listfile)
i=0
while i<len(listf)-2:
intlow=int(listf[i],16)
inthigh=int(listf[i+1],16)
i=i+2
num2=intlow+(inthigh<<8)
if (inthigh>>7)&0x01==0x01:
num2-=65536
file_write.write(str(num2)+'\n');
#file_write.write("\n");
#listf中的数据类型为字符串,需要转为16进制对应的数据
file_open.close()
file_write.close()
print("Data processing is completed")
.
代码如图:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
namespace HandleData
{
class Program
{
static void Main(string[] args)
{
string line = string.Empty;
HandleData.Program.ReadData("E:\\language\\python\\file\\data.txt", "E:\\language\\python\\file\\dataResult.txt");
Console.ReadLine();
}
static void ReadData(string filepath,string filepathw)
{
string strData = File.ReadAllText(filepath);
FileStream fs = File.Open(filepathw, FileMode.Create);
string[] str = strData.Split(' ','\r','\n');
StreamWriter wr = new StreamWriter(fs);
for (int k=0;k<str.Count()-2;k+=2)
{
string strA = str[k] +" "+ str[k+1];
int low;
//
if (str[k] == "")
{
k++;
}
low = Convert.ToInt16(str[k], 16);
if (str[k + 1] == "")
{
k++;
}
int high = Convert.ToInt16(str[k + 1], 16);
byte[] bytearr= { (byte)low, (byte)high };
int result = BitConverter.ToInt16(bytearr,0);
wr.WriteLine(result);
//byte[] byteArray = System.Text.Encoding.Default.GetBytes(strA);
}
wr.Flush();
wr.Close();
// 关闭文件
fs.Close();
Console.WriteLine(strData);
}
}
}
界面:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace HandleTxtData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
ofd.ValidateNames = true;
ofd.CheckPathExists = true;
ofd.CheckFileExists = true;
string strFileName="";
if (ofd.ShowDialog() == DialogResult.OK)
{
strFileName = ofd.FileName;
}
textBox1.Text = strFileName;
string path = Path.GetDirectoryName(strFileName);
string fileName = Path.GetFileNameWithoutExtension(strFileName);
string strFileNameW = path +"\\"+fileName+ "result.txt";
ReadData(strFileName,strFileNameW);
}
public void ReadData(string filepath, string filepathw)
{
string strData = File.ReadAllText(filepath);
FileStream fs = File.Open(filepathw, FileMode.OpenOrCreate);
string[] str = strData.Split(' ', '\r', '\n');
StreamWriter wr = new StreamWriter(fs);
richTextBox1.Text = "开始处理数据\n";
for (int k = 0; k < str.Count() - 2; k += 2)
{
string strA = str[k] + " " + str[k + 1];
int low;
//
if (str[k] == "")
{
k++;
}
low = Convert.ToInt16(str[k], 16);
if (str[k + 1] == "")
{
k++;
}
int high = Convert.ToInt16(str[k + 1], 16);
byte[] bytearr = { (byte)low, (byte)high };
int result = BitConverter.ToInt16(bytearr, 0);
wr.WriteLine(result);
//byte[] byteArray = System.Text.Encoding.Default.GetBytes(strA);
richTextBox1.Text = ".";
}
richTextBox1.Text = "\n数据处理完成";
wr.Flush();
wr.Close();
// 关闭文件
fs.Close();
Console.WriteLine(strData);
}
}
}
用python语言试一下,发现处理数据更快,代码如下:
#encoding=utf-8
import re
file_open=open('data.txt')
file_write=open('handle_data.txt','w')
listfile=file_open.read()
listf=re.split('[ \n\r]',listfile)
i=0
while i<len(listf)-2:
intlow=int(listf[i],16)
inthigh=int(listf[i+1],16)
i=i+2
num2=intlow+(inthigh<<8)
if (inthigh>>7)&0x01==0x01:
num2-=65536
file_write.write(str(num2));
file_write.write("\n");
#listf中的数据类型为字符串,需要转为16进制对应的数据
file_open.close()
file_write.close()
print "数据处理完成"
将.py修改为可以添加参数的脚本,在cmd路径在,输入python,拖入.py文件,再拖入要处理的data.txt,会在同一路径下生成Result.txt结果文件.代码如下#encoding=utf-8
import re
import os
import sys
file_open=open(sys.argv[1])#处理输入的文件参数
filewritepath=os.path.dirname(sys.argv[1])+"\\Result.txt"
file_write=open(filewritepath,'w')
#file_open=open('data.txt')
#file_write=open('handle_data.txt','w')
listfile=file_open.read()
listf=re.split('[ \n\r]',listfile)
i=0
while i<len(listf)-2:
intlow=int(listf[i],16)
inthigh=int(listf[i+1],16)
i=i+2
num2=intlow+(inthigh<<8)
if (inthigh>>7)&0x01==0x01:
num2-=65536
file_write.write(str(num2)+'\n');
#file_write.write("\n");
#listf中的数据类型为字符串,需要转为16进制对应的数据
file_open.close()
file_write.close()
print("Data processing is completed")
.
相关文章推荐
- 【python图像处理】txt文件数据的读取与写入
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- python读取.txt文件数据和将数据写入文件.txt
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- MATLAB处理txt文本文件---数据格式要有规律性,否则要用编写特定方式进行读取
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C#控制台基础 File.WriteAllText,以覆盖的方式将一个string写入txt
- C#控制台控制台将一个文件夹中的所有txt文件中的数据读取出来
- ean13码的生成,python读取csv中数据并处理返回并写入到另一个csv文件中
- C#编程:从控制台读取数字的两种方式
- C#控制台控制台将一个文件夹中的所有txt文件中的数据读取出来
- matlab处理txt文本文件---数据格式要有规律性,否则要用编写特定方式进行读取
- python3将csv文件中的两列数据读取出来,并且按行写入txt文本之中
- asp.net(c#)如何读取上传过程中的.txt文件中的数据,并将其写入数据库的
- java 读取文件——按照行取出(使用BufferedReader和一次将数据保存到内存两种实现方式)
- python 读取文件、并以十六进制的方式写入到新文件
- C# DAL添加数据的两种处理方式